提交 639f55d7 authored 作者: mooncake's avatar mooncake

update

上级 a71424db
...@@ -4,6 +4,7 @@ package response ...@@ -4,6 +4,7 @@ package response
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
...@@ -267,17 +268,17 @@ func Error(c *gin.Context, err error) { ...@@ -267,17 +268,17 @@ func Error(c *gin.Context, err error) {
c.Set(ctxutils.KeyRspCode, 0) c.Set(ctxutils.KeyRspCode, 0)
msg := "" msg := ""
if err != nil { if err != nil {
msg = err.Error() switch e := err.(type) {
if e, ok := err.(*xerror.Error); ok { case *xerror.BizError, *xerror.ParamError:
//if !errcode.IsSysDefinedError(xerr.Code()) { msg = e.Error()
// logger.Error(err.Error(), logger.Err(err), ctxutils.GinTraceIDField(c)) case *xerror.DBError, *xerror.NetError:
//} logger.Error(fmt.Sprintf("[response]%s", err.Error()), ctxutils.GinTraceIDField(c))
if e.Code() != errcode.InvalidParams.Code() { msg = "网络开小差了,请稍后再试~"
logger.Error(err.Error(), logger.Err(err), ctxutils.GinTraceIDField(c)) default:
} msg = e.Error()
} else { logger.Error(fmt.Sprintf("[response]%s", err.Error()), ctxutils.GinTraceIDField(c))
logger.Error(err.Error(), logger.Err(err), ctxutils.GinTraceIDField(c))
} }
} }
respJSONWith200(c, CustomErrorCode, msg) respJSONWith200(c, CustomErrorCode, msg)
} }
...@@ -8,16 +8,48 @@ import ( ...@@ -8,16 +8,48 @@ import (
// New 用于创建一个自定义文本错误信息的 error 对象,并包含堆栈信息。 // New 用于创建一个自定义文本错误信息的 error 对象,并包含堆栈信息。
func New(text string) error { func New(text string) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: text, text: text,
code: xcode.CodeDefault, code: xcode.CodeDefault,
} }
} }
func NewNetError(text string) error {
e := NetError{}
e.stack = callers()
e.text = text
e.code = xcode.CodeDefault
return &e
}
func NewDBError(text string) error {
e := DBError{}
e.stack = callers()
e.text = text
e.code = xcode.CodeDefault
return &e
}
func NewParamError(text string) error {
e := ParamError{}
e.stack = callers()
e.text = text
e.code = xcode.CodeDefault
return &e
}
func NewBizError(text string) error {
e := BizError{}
e.stack = callers()
e.text = text
e.code = xcode.CodeDefault
return &e
}
// NewC add code // NewC add code
func NewC(code int, text string) error { func NewC(code int, text string) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: text, text: text,
code: code, code: code,
...@@ -49,21 +81,21 @@ func Join(errs ...error) error { ...@@ -49,21 +81,21 @@ func Join(errs ...error) error {
// Newf 用于创建一个自定义文本错误带参数信息的 error 对象,并包含堆栈信息。 // Newf 用于创建一个自定义文本错误带参数信息的 error 对象,并包含堆栈信息。
func Newf(format string, args ...any) error { func Newf(format string, args ...any) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: xcode.CodeDefault, code: xcode.CodeDefault,
} }
} }
func Errorf(format string, args ...any) error { func Errorf(format string, args ...any) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: xcode.CodeDefault, code: xcode.CodeDefault,
} }
} }
func NewCf(code int, format string, args ...any) error { func NewCf(code int, format string, args ...any) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: code, code: code,
...@@ -73,7 +105,7 @@ func NewCf(code int, format string, args ...any) error { ...@@ -73,7 +105,7 @@ func NewCf(code int, format string, args ...any) error {
// NewSkip 用于创建一个自定义错误信息的 error 对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。高级功能,一般开发者很少用得到。 // NewSkip 用于创建一个自定义错误信息的 error 对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。高级功能,一般开发者很少用得到。
// 参数 `skip` 指定堆栈跳过的层数。 // 参数 `skip` 指定堆栈跳过的层数。
func NewSkip(skip int, text string) error { func NewSkip(skip int, text string) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: text, text: text,
code: xcode.CodeDefault, code: xcode.CodeDefault,
...@@ -81,7 +113,7 @@ func NewSkip(skip int, text string) error { ...@@ -81,7 +113,7 @@ func NewSkip(skip int, text string) error {
} }
func NewSkipC(code, skip int, text string) error { func NewSkipC(code, skip int, text string) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: text, text: text,
code: code, code: code,
...@@ -91,7 +123,7 @@ func NewSkipC(code, skip int, text string) error { ...@@ -91,7 +123,7 @@ func NewSkipC(code, skip int, text string) error {
// NewSkipf 用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。 // NewSkipf 用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。
// 参数 `skip` 指定堆栈跳过的层数。 // 参数 `skip` 指定堆栈跳过的层数。
func NewSkipf(skip int, format string, args ...any) error { func NewSkipf(skip int, format string, args ...any) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: xcode.CodeDefault, code: xcode.CodeDefault,
...@@ -99,7 +131,7 @@ func NewSkipf(skip int, format string, args ...any) error { ...@@ -99,7 +131,7 @@ func NewSkipf(skip int, format string, args ...any) error {
} }
func NewSkipCf(code, skip int, format string, args ...any) error { func NewSkipCf(code, skip int, format string, args ...any) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: code, code: code,
...@@ -112,7 +144,7 @@ func Wrap(err error, text string) error { ...@@ -112,7 +144,7 @@ func Wrap(err error, text string) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(), stack: callers(),
text: text, text: text,
...@@ -125,7 +157,7 @@ func Wrapf(err error, format string, args ...any) error { ...@@ -125,7 +157,7 @@ func Wrapf(err error, format string, args ...any) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
...@@ -138,7 +170,7 @@ func WrapSkip(skip int, err error, text string) error { ...@@ -138,7 +170,7 @@ func WrapSkip(skip int, err error, text string) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(skip), stack: callers(skip),
text: text, text: text,
...@@ -151,7 +183,7 @@ func WrapSkipf(skip int, err error, format string, args ...any) error { ...@@ -151,7 +183,7 @@ func WrapSkipf(skip int, err error, format string, args ...any) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(skip), stack: callers(skip),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
// NewCode 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。 // NewCode 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。
func NewCode(code int, text ...string) error { func NewCode(code int, text ...string) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: strings.Join(text, separatorSpace), text: strings.Join(text, separatorSpace),
code: code, code: code,
...@@ -18,7 +18,7 @@ func NewCode(code int, text ...string) error { ...@@ -18,7 +18,7 @@ func NewCode(code int, text ...string) error {
// NewCodef 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。 // NewCodef 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。
func NewCodef(code int, format string, args ...any) error { func NewCodef(code int, format string, args ...any) error {
return &Error{ return &Err{
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: code, code: code,
...@@ -27,7 +27,7 @@ func NewCodef(code int, format string, args ...any) error { ...@@ -27,7 +27,7 @@ func NewCodef(code int, format string, args ...any) error {
// NewCodeSkip 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。 // NewCodeSkip 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。
func NewCodeSkip(code int, skip int, text ...string) error { func NewCodeSkip(code int, skip int, text ...string) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: strings.Join(text, separatorSpace), text: strings.Join(text, separatorSpace),
code: code, code: code,
...@@ -36,7 +36,7 @@ func NewCodeSkip(code int, skip int, text ...string) error { ...@@ -36,7 +36,7 @@ func NewCodeSkip(code int, skip int, text ...string) error {
// NewCodeSkipf 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。 // NewCodeSkipf 用于创建一个自定义错误信息的 error 对象,并包含堆栈信息,并增加错误码对象的输入。并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。
func NewCodeSkipf(code int, skip int, format string, args ...any) error { func NewCodeSkipf(code int, skip int, format string, args ...any) error {
return &Error{ return &Err{
stack: callers(skip), stack: callers(skip),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
code: code, code: code,
...@@ -48,7 +48,7 @@ func WrapCode(code int, err error, text ...string) error { ...@@ -48,7 +48,7 @@ func WrapCode(code int, err error, text ...string) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(), stack: callers(),
text: strings.Join(text, separatorSpace), text: strings.Join(text, separatorSpace),
...@@ -61,7 +61,7 @@ func WrapCodef(code int, err error, format string, args ...any) error { ...@@ -61,7 +61,7 @@ func WrapCodef(code int, err error, format string, args ...any) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(), stack: callers(),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
...@@ -74,7 +74,7 @@ func WrapCodeSkip(code int, skip int, err error, text ...string) error { ...@@ -74,7 +74,7 @@ func WrapCodeSkip(code int, skip int, err error, text ...string) error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(skip), stack: callers(skip),
text: strings.Join(text, separatorSpace), text: strings.Join(text, separatorSpace),
...@@ -87,7 +87,7 @@ func WrapCodeSkipf(code int, skip int, err error, format string, args ...any) er ...@@ -87,7 +87,7 @@ func WrapCodeSkipf(code int, skip int, err error, format string, args ...any) er
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: err, error: err,
stack: callers(skip), stack: callers(skip),
text: fmt.Sprintf(format, args...), text: fmt.Sprintf(format, args...),
......
...@@ -10,7 +10,7 @@ type Option struct { ...@@ -10,7 +10,7 @@ type Option struct {
// NewOption 用于自定义配置的错误对象创建。 // NewOption 用于自定义配置的错误对象创建。
func NewOption(option Option) error { func NewOption(option Option) error {
err := &Error{ err := &Err{
error: option.Error, error: option.Error,
text: option.Text, text: option.Text,
code: option.Code, code: option.Code,
......
...@@ -8,14 +8,30 @@ import ( ...@@ -8,14 +8,30 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// Error 自定义错误对象。 // Err 自定义错误对象。
type Error struct { type Err struct {
error error // 包装错误。 error error // 包装错误。
stack stack // 堆栈数组,当创建或包装此错误时记录堆栈信息。 stack stack // 堆栈数组,当创建或包装此错误时记录堆栈信息。
text string // 创建错误时自定义错误文本。 text string // 创建错误时自定义错误文本。
code int // 如有必要,错误码。 code int // 如有必要,错误码。
} }
type NetError struct {
Err
}
type DBError struct {
Err
}
type ParamError struct {
Err
}
type BizError struct {
Err
}
const ( const (
// stackFilterKeyLocal 过滤当前错误模块路径的键。 // stackFilterKeyLocal 过滤当前错误模块路径的键。
stackFilterKeyLocal = "/xerrors/xerror" stackFilterKeyLocal = "/xerrors/xerror"
...@@ -33,7 +49,7 @@ func init() { ...@@ -33,7 +49,7 @@ func init() {
} }
// Error 实现错误的接口,它将所有错误返回为字符串。 // Error 实现错误的接口,它将所有错误返回为字符串。
func (err *Error) Error() string { func (err *Err) Error() string {
if err == nil { if err == nil {
return "" return ""
} }
...@@ -48,14 +64,14 @@ func (err *Error) Error() string { ...@@ -48,14 +64,14 @@ func (err *Error) Error() string {
} }
// Cause 获取根错误 error。 // Cause 获取根错误 error。
func (err *Error) Cause() error { func (err *Err) Cause() error {
if err == nil { if err == nil {
return nil return nil
} }
loop := err loop := err
for loop != nil { for loop != nil {
if loop.error != nil { if loop.error != nil {
if e, ok := loop.error.(*Error); ok { if e, ok := loop.error.(*Err); ok {
// 内部自定义错误。 // 内部自定义错误。
loop = e loop = e
} else if e, ok := loop.error.(ICause); ok { } else if e, ok := loop.error.(ICause); ok {
...@@ -76,11 +92,11 @@ func (err *Error) Cause() error { ...@@ -76,11 +92,11 @@ func (err *Error) Cause() error {
// Current 获取当前 error。 // Current 获取当前 error。
// 如果当前错误是 nil, 则返回 nil。 // 如果当前错误是 nil, 则返回 nil。
func (err *Error) Current() error { func (err *Err) Current() error {
if err == nil { if err == nil {
return nil return nil
} }
return &Error{ return &Err{
error: nil, error: nil,
stack: err.stack, stack: err.stack,
text: err.text, text: err.text,
...@@ -89,7 +105,7 @@ func (err *Error) Current() error { ...@@ -89,7 +105,7 @@ func (err *Error) Current() error {
} }
// Unwrap 获取下一层 error。 // Unwrap 获取下一层 error。
func (err *Error) Unwrap() error { func (err *Err) Unwrap() error {
if err == nil { if err == nil {
return nil return nil
} }
...@@ -98,7 +114,7 @@ func (err *Error) Unwrap() error { ...@@ -98,7 +114,7 @@ func (err *Error) Unwrap() error {
// Equal 错误对象比较。 // Equal 错误对象比较。
// 如果它们的 `code` 和 `text` 都相同,则认为错误相同。 // 如果它们的 `code` 和 `text` 都相同,则认为错误相同。
func (err *Error) Equal(target error) bool { func (err *Err) Equal(target error) bool {
if err.Error() == target.Error() { if err.Error() == target.Error() {
return true return true
} }
...@@ -112,7 +128,7 @@ func (err *Error) Equal(target error) bool { ...@@ -112,7 +128,7 @@ func (err *Error) Equal(target error) bool {
} }
// Is 当前错误 `err` 的链接错误中是否包含错误 `target`。 // Is 当前错误 `err` 的链接错误中是否包含错误 `target`。
func (err *Error) Is(target error) bool { func (err *Err) Is(target error) bool {
if Equal(err, target) { if Equal(err, target) {
return true return true
} }
......
...@@ -4,7 +4,7 @@ import "gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xcode" ...@@ -4,7 +4,7 @@ import "gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xcode"
// Code 获取错误码。 // Code 获取错误码。
// 如果没有错误代码,则返回 `xcode.CodeDefault`。 // 如果没有错误代码,则返回 `xcode.CodeDefault`。
func (err *Error) Code() int { func (err *Err) Code() int {
if err == nil { if err == nil {
return xcode.CodeDefault return xcode.CodeDefault
} }
...@@ -15,7 +15,7 @@ func (err *Error) Code() int { ...@@ -15,7 +15,7 @@ func (err *Error) Code() int {
} }
// SetCode 使用指定 `code` 更新内部 `code` 。 // SetCode 使用指定 `code` 更新内部 `code` 。
func (err *Error) SetCode(code int) { func (err *Err) SetCode(code int) {
if err == nil { if err == nil {
return return
} }
......
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
// %-v, %-s : 打印当前级别错误字符串; // %-v, %-s : 打印当前级别错误字符串;
// %+s : 打印完整堆栈错误列表; // %+s : 打印完整堆栈错误列表;
// %+v : 打印错误字符串和完整堆栈错误列表 // %+v : 打印错误字符串和完整堆栈错误列表
func (err *Error) Format(s fmt.State, verb rune) { func (err *Err) Format(s fmt.State, verb rune) {
switch verb { switch verb {
case 's', 'v': case 's', 'v':
switch { switch {
......
...@@ -2,6 +2,6 @@ package xerror ...@@ -2,6 +2,6 @@ package xerror
// MarshalJSON 实现 json.Marshal 接口。 // MarshalJSON 实现 json.Marshal 接口。
// 注:这里不要使用指针作为其接收器。 // 注:这里不要使用指针作为其接收器。
func (err Error) MarshalJSON() ([]byte, error) { func (err Err) MarshalJSON() ([]byte, error) {
return []byte(`"` + err.Error() + `"`), nil return []byte(`"` + err.Error() + `"`), nil
} }
...@@ -22,7 +22,7 @@ type stackLine struct { ...@@ -22,7 +22,7 @@ type stackLine struct {
} }
// Stack 以字符串形式返回错误堆栈信息。 // Stack 以字符串形式返回错误堆栈信息。
func (err *Error) Stack() string { func (err *Err) Stack() string {
if err == nil { if err == nil {
return "" return ""
} }
...@@ -40,7 +40,7 @@ func (err *Error) Stack() string { ...@@ -40,7 +40,7 @@ func (err *Error) Stack() string {
infos = append(infos, info) infos = append(infos, info)
loopLinesOfStackInfo(loop.stack, info) loopLinesOfStackInfo(loop.stack, info)
if loop.error != nil { if loop.error != nil {
if e, ok := loop.error.(*Error); ok { if e, ok := loop.error.(*Err); ok {
loop = e loop = e
} else { } else {
infos = append(infos, &stackInfo{ infos = append(infos, &stackInfo{
......
...@@ -221,7 +221,7 @@ func Test_SetCode(t *testing.T) { ...@@ -221,7 +221,7 @@ func Test_SetCode(t *testing.T) {
assert.Equal(t, xerror.Code(err), -1) assert.Equal(t, xerror.Code(err), -1)
assert.Equal(t, err.Error(), "123") assert.Equal(t, err.Error(), "123")
err.(*xerror.Error).SetCode(xcode.CodeValidationFailed) err.(*xerror.Err).SetCode(xcode.CodeValidationFailed)
assert.Equal(t, xerror.Code(err), xcode.CodeValidationFailed) assert.Equal(t, xerror.Code(err), xcode.CodeValidationFailed)
assert.Equal(t, err.Error(), "123") assert.Equal(t, err.Error(), "123")
} }
......
...@@ -2,7 +2,6 @@ package xcommon ...@@ -2,7 +2,6 @@ package xcommon
import ( import (
"context" "context"
"errors"
"net/http" "net/http"
"os" "os"
"reflect" "reflect"
...@@ -57,13 +56,14 @@ func (e *Controller) Bind(c *gin.Context, req any) error { ...@@ -57,13 +56,14 @@ func (e *Controller) Bind(c *gin.Context, req any) error {
if valid, ok := req.(IValid); ok { if valid, ok := req.(IValid); ok {
if err := valid.Valid(); err != nil { if err := valid.Valid(); err != nil {
return err return xerror.NewParamError(err.Error())
} }
} }
storePagination(req, reflectPagination(c)) storePagination(req, reflectPagination(c))
c.Set("reqbody", req) c.Set("reqbody", req)
return nil return nil
} }
...@@ -106,9 +106,9 @@ func zhError(err error) error { ...@@ -106,9 +106,9 @@ func zhError(err error) error {
strs = append(strs, zhStr) strs = append(strs, zhStr)
} }
if len(strs) > 0 { if len(strs) > 0 {
return errors.New(strings.Join(strs, ";")) return xerror.NewParamError(strings.Join(strs, ";"))
} else { } else {
return xerror.Join(errs) return xerror.NewParamError(xerror.Join(errs).Error())
} }
} }
return err return err
......
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论