Commit 5205d2de authored by kzkzzzz's avatar kzkzzzz

feat: 修改错误

parent 69953656
......@@ -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)
}
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