From 5205d2de5caa438d92263ba61e0e9eeb625a3020 Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Fri, 10 Jun 2022 17:10:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/errorx/error.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/common/errorx/error.go b/common/errorx/error.go index c2b3743..20cf33d 100644 --- a/common/errorx/error.go +++ b/common/errorx/error.go @@ -6,11 +6,20 @@ import ( "gomicro-base/common/validate" ) +type code int32 + const ( - DefaultError = 500 - ParamsError = 400 + DefaultErrorCode int32 = 1000 + ParamsErrorCode = 1001 + NotFoundErrorCode = 1002 ) +var codeStatusText = map[int32]string{ + DefaultErrorCode: "操作失败", + ParamsErrorCode: "参数不正确", + NotFoundErrorCode: "未查询到记录", +} + type ResponseError struct { Code int `json:"code"` Msg string `json:"msg"` @@ -22,22 +31,22 @@ func (c *ResponseError) Error() string { return fmt.Sprintf("[%d]%s", c.Code, c.Msg) } -func NewMicroError(detail string, code ...int) error { - var c = DefaultError +func NewMicroError(detail string, code ...int32) error { + var c = DefaultErrorCode if len(code) > 0 { c = code[0] } return &errors.Error{ - Code: int32(c), + Code: c, Detail: detail, - Status: "操作失败", + Status: codeStatusText[c], } } -func NewParamsError(err error) error { - return &errors.Error{ - Code: int32(ParamsError), - Detail: validate.TransError(err), - Status: "参数不正确", - } +func TransParamsError(err error) error { + return NewMicroError(validate.TransError(err), ParamsErrorCode) +} + +func NotFound(detail string) error { + return NewMicroError(detail, NotFoundErrorCode) } -- 2.22.0