Skip to content

Commit

Permalink
<feat>(version):1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
hny committed Apr 7, 2024
0 parents commit 99e58c5
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}

```
25 changes: 25 additions & 0 deletions code.go
Original file line number Diff line number Diff line change
@@ -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
}
160 changes: 160 additions & 0 deletions err.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions err_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hny/err

go 1.21.4
20 changes: 20 additions & 0 deletions msg.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 99e58c5

Please sign in to comment.