Skip to content

Commit

Permalink
enhance(error): with params
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Nov 28, 2024
1 parent 7313d9b commit 659fe23
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
39 changes: 39 additions & 0 deletions docs/error-handler/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,42 @@ var (
```

届时,前端可以根据获取到的 `scope``code` 来进行相应的处理。

## 附加参数
`v1.12.0` 中,我们新增了一个 `cosy.NewErrorWithParams` 方法,用于在创建错误时附加额外的参数。

```go
func NewErrorWithParams(code int32, message string, params ...string) error
```

需要注意的是,message 的内容需要包含 `{index}` 作为参数的占位符,否则在控制台的打印中不能输出正确的错误信息,例如:
```go
e = cosy.NewErrorScope("tracking")

cErr := e.NewWithParams(500, "跟踪信息 {0} 的错误是 {1}", "foo", "bar")
```

当使用 `logger` 打印时候,它将会输出:
```
跟踪信息 foo 的错误是 bar
```

使用 `cosy.Errhander` 处理时,会响应:
```json
{
"scope": "tracking",
"code": 500,
"message": "跟踪信息 {0} 的错误是 {1}",
"params": ["foo", "bar"]
}
```

您也可以直接使用 `cosy.NewErrorWithParams` 来创建一个无作用域的错误,在使用 `cosy.ErrHandler` 处理时,会响应:
```json
{
"code": 500,
"message": "跟踪信息 {0} 的错误是 {1}",
"params": ["foo", "bar"]
}
```
39 changes: 35 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cosy

import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/logger"
"github.com/uozi-tech/cosy/settings"
"go.uber.org/zap"
"gorm.io/gorm"
"net/http"
"strings"
)

type ErrorScope struct {
Expand All @@ -18,6 +20,7 @@ func NewErrorScope(scope string) *ErrorScope {
return &ErrorScope{scope}
}

// New create a new error with scope
func (s *ErrorScope) New(code int32, message string) error {
return &Error{
Scope: s.scope,
Expand All @@ -26,23 +29,51 @@ func (s *ErrorScope) New(code int32, message string) error {
}
}

// NewWithParams create a new error with scope and params
func (s *ErrorScope) NewWithParams(code int32, message string, params ...string) error {
return &Error{
Scope: s.scope,
Code: code,
Message: message,
Params: params,
}
}

type Error struct {
Scope string `json:"scope,omitempty"`
Code int32 `json:"code"`
Message string `json:"message"`
Scope string `json:"scope,omitempty"`
Code int32 `json:"code"`
Message string `json:"message"`
Params []string `json:"params,omitempty"`
}

func (e *Error) Error() string {
return e.Message
if len(e.Params) == 0 {
return e.Message
}
msg := e.Message
for index, param := range e.Params {
msg = strings.Replace(msg, fmt.Sprintf("{%d}", index), param, 1)
}
return msg
}

// NewError create a new error
func NewError(code int32, message string) error {
return &Error{
Code: code,
Message: message,
}
}

// NewErrorWithParams create a new error with params
func NewErrorWithParams(code int32, message string, params ...string) error {
return &Error{
Code: code,
Message: message,
Params: params,
}
}

// errorResp error response
func errorResp(c *gin.Context, err error) {
var cErr *Error
Expand Down

0 comments on commit 659fe23

Please sign in to comment.