Commit 5205d2de authored by kzkzzzz's avatar kzkzzzz

feat: 修改错误

parent 69953656
...@@ -6,11 +6,20 @@ import ( ...@@ -6,11 +6,20 @@ import (
"gomicro-base/common/validate" "gomicro-base/common/validate"
) )
type code int32
const ( const (
DefaultError = 500 DefaultErrorCode int32 = 1000
ParamsError = 400 ParamsErrorCode = 1001
NotFoundErrorCode = 1002
) )
var codeStatusText = map[int32]string{
DefaultErrorCode: "操作失败",
ParamsErrorCode: "参数不正确",
NotFoundErrorCode: "未查询到记录",
}
type ResponseError struct { type ResponseError struct {
Code int `json:"code"` Code int `json:"code"`
Msg string `json:"msg"` Msg string `json:"msg"`
...@@ -22,22 +31,22 @@ func (c *ResponseError) Error() string { ...@@ -22,22 +31,22 @@ func (c *ResponseError) Error() string {
return fmt.Sprintf("[%d]%s", c.Code, c.Msg) return fmt.Sprintf("[%d]%s", c.Code, c.Msg)
} }
func NewMicroError(detail string, code ...int) error { func NewMicroError(detail string, code ...int32) error {
var c = DefaultError var c = DefaultErrorCode
if len(code) > 0 { if len(code) > 0 {
c = code[0] c = code[0]
} }
return &errors.Error{ return &errors.Error{
Code: int32(c), Code: c,
Detail: detail, Detail: detail,
Status: "操作失败", Status: codeStatusText[c],
} }
} }
func NewParamsError(err error) error { func TransParamsError(err error) error {
return &errors.Error{ return NewMicroError(validate.TransError(err), ParamsErrorCode)
Code: int32(ParamsError), }
Detail: validate.TransError(err),
Status: "参数不正确", func NotFound(detail string) error {
} return NewMicroError(detail, NotFoundErrorCode)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment