diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8d10409 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Bubgit + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..03335ba --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +### Quick use: +```golang + +import Err "github.com/InkAndHumour/err" + +func foo() Err.Err { + err := …… + if err != nil { + return Err.New().Err( + "……",err, + ) + } + + return nil +} + +``` + +### if you wangt to return the default error: +```golang + +import Err "github.com/InkAndHumour/err" + +func foo() error { + err := …… + if err != nil { + return Err.New().Err( + "……",err, + ) + } + + return nil +} + +``` \ No newline at end of file diff --git a/code.go b/code.go new file mode 100644 index 0000000..670058e --- /dev/null +++ b/code.go @@ -0,0 +1,25 @@ +package err + +type code uint32 + +const ( + // 创建资源失败 + CodeCreateFialed code = 1001001 + // 记录不存在 + CodeItemNotFound code = 1001002 + // 更新记录失败 + CodeUpdateFialed code = 1001003 + // 删除记录失败 + CodeDeleteFialed code = 1001004 + + // 记录已过期 + CodeItemExpired code = 2001001 + + // 资源繁忙 + CodeIsBusy code = 3001001 +) + +func (e *errs) Code(code uint32) Err { + e.code = code + return e +} diff --git a/err.go b/err.go new file mode 100644 index 0000000..0138309 --- /dev/null +++ b/err.go @@ -0,0 +1,160 @@ +package err + +import ( + "fmt" +) + +const ( + // "incorrect params value" 参数值错误 + ERR_PARAMS = "incorrect params value" + // "lack of necessary parameters" 必填参数不可为空 + ERR_NECESSARY_PARAMS = "lack of necessary parameters" + // "item info not exist" 记录信息不存在 + ERR_ITEM_NOT_EXIST = "item info not exist" + // "effect rows is 0" 操作影响数为 0 + ERR_EFFECT_ROWS_0 = "effect rows is 0" + + // 请勿重复请求 + ERR_LIMIT_DUPLICATED_CODE = 10001 + ERR_LIMIT_DUPLICATED_ZN = "请勿重复请求" + // 请求去重校验失败 + ERR_LIMIT_DUPLICATION_FAILED_CODE = 10002 + ERR_LIMIT_DUPLICATION_FAILED_ZN = "请求去重校验失败" + + // 参数有误,请检查 + ERR_INVALIDED_PARAMS_CODE = 10010 + ERR_INVALIDED_PARAMS_ZN = "参数有误,请检查" + // 记录已存在 + ERR_ALREADY_EXIST_CODE = 10011 + ERR_ALREADY_EXIST_ZN = "记录已存在" + + // 用户名或密码错误 + ERR_INVALIDED_USERNAME_PASSWORD_CODE = 10020 + ERR_INVALIDED_USERNAME_PASSWORD_ZN = "用户名或密码错误" + // Token 无效 + ERR_INVALIDED_TOKEN_CODE = 10021 + ERR_INVALIDED_TOKEN_ZN = "Token 无效" + // "仅支持系统管理员" + ERR_ONLY_FOR_ADMIN_CODE = 10022 + ERR_ONLY_FOR_ADMIN_ZN = "仅支持系统管理员" + + // 内部系统错误 + ERR_SYS_BUSY_CODE = 50001 + ERR_SYS_BUSY_ZN = "系统繁忙请稍后" + ERR_SYS_INNER_CODE = 50010 + ERR_SYS_INNER_ZN = "服务异常,请稍后" +) + +type Err interface { + // set or add error for debug + Err(cue string, detail ...interface{}) Err + // attach string: faield, to explain the call path + Failed(cue string) Err + // set or add context value for debug + Info(cue string, detail ...interface{}) Err + // set or add error code for front + Code(code uint32) Err + // set or add cue msg for front + Msg(msg string) Err + // return full error detail for debug + Detail() error + GetMsg() string + GetCode() string + GetCodeUint32() uint32 + GetCodeInt32() int32 + + Error() string +} + +func New() Err { + return &errs{} +} + +type errs struct { + err error + code uint32 + msg string +} + +func (e *errs) Error() string { + return fmt.Sprintf("{\"err\":\"%+v\",\"msg\":\"%v\",\"code\":\"%v\"}", e.err, e.msg, e.code) +} +func (e *errs) GetMsg() string { + return fmt.Sprintf("%v", e.msg) +} + +func (e *errs) GetCode() string { + return fmt.Sprintf("%v", e.code) +} +func (e *errs) GetCodeUint32() uint32 { + return e.code +} +func (e *errs) GetCodeInt32() int32 { + return int32(e.code) +} + +func (e *errs) Detail() error { + return fmt.Errorf("{\"err\":\"%+v\",\"msg\":\"%v\",\"code\":\"%v\"}", e.err, e.msg, e.code) +} + +func (e *errs) Err(cue string, detail ...interface{}) Err { + if e.err != nil { + return e.add(cue, detail...) + } + return e.set(cue, detail...) +} + +func (e *errs) Failed(cue string) Err { + return e.Err(fmt.Sprintf("%v failed", cue)) +} + +func (e *errs) set(cue string, detail ...interface{}) *errs { + if len(detail) == 0 { + return e.setSolo(cue) + } + return e.setMulti(cue, detail[0]) +} + +func (e *errs) setSolo(cue string) *errs { + e.err = fmt.Errorf("%v;", cue) + return e +} + +func (e *errs) setMulti(cue string, detail interface{}) *errs { + e.err = fmt.Errorf("%v err: %v;", cue, detail) + return e +} + +func (e *errs) add(cue string, detail ...interface{}) *errs { + if len(detail) == 0 { + return e.addSolo(cue) + } + return e.addMulti(cue, detail[0]) +} + +func (e *errs) addSolo(cue string) *errs { + e.err = fmt.Errorf("%v; %v", cue, e.err) + return e +} + +func (e *errs) addMulti(cue string, detail interface{}) *errs { + e.err = fmt.Errorf(" %v err: %+v; %v ", cue, detail, e.err) + return e +} + +func (e *errs) Info(cue string, detail ...interface{}) Err { + if e.err != nil { + return e.addInfo(cue, detail) + } + return e.setInfo(cue, detail) +} + +func (e *errs) setInfo(cue string, detail interface{}) *errs { + e.err = fmt.Errorf("%v: %+v;", cue, detail) + return e +} + +func (e *errs) addInfo(cue string, detail interface{}) *errs { + e.err = fmt.Errorf("%v %v: %+v;", e.err, cue, detail) + return e +} diff --git a/err_test.go b/err_test.go new file mode 100644 index 0000000..eb35868 --- /dev/null +++ b/err_test.go @@ -0,0 +1,19 @@ +package err + +import "testing" + +func TestErr(t *testing.T) { + // err := &errs{} + + // err.Err("c1", "e1") + // t.Log("setSolo:", err.Detail()) + + // err.Err("c2", "e2") + // t.Log("setSolo:", err.Detail()) + + // err.Err("c3", "e3") + // t.Log("setSolo:", err.Detail()) + + err := New() + t.Log("err.GetCode():", err.GetCode()) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2f1ecdc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/hny/err + +go 1.21.4 diff --git a/msg.go b/msg.go new file mode 100644 index 0000000..05daf8c --- /dev/null +++ b/msg.go @@ -0,0 +1,20 @@ +package err + +import "fmt" + +func (e *errs) Msg(msg string) Err { + if e.msg == "" { + return e.setMsg(msg) + } + return e.addMsg(msg) +} + +func (e *errs) setMsg(msg string) *errs { + e.msg = msg + return e +} + +func (e *errs) addMsg(msg string) *errs { + e.msg = fmt.Sprintf("%s; %s", e.msg, msg) + return e +}