error.go 563 Bytes
Newer Older
彭芳 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package errs

import "fmt"

var (
	ErrorArgs    = New(40001, "请求参数错误")
	ServiceError = New(50000, "系统错误请稍后再试。")
)

type Error struct {
	Code int    // 错误码
	Msg  string // 错误信息
}

func (e *Error) Error() string {
	return e.Msg
}

func (e *Error) WithMessage(msg string) *Error {
	out := &Error{
		Code: e.Code,
		Msg:  e.Msg,
	}
	if out.Msg == "" {
		out.Msg = msg
	} else {
		out.Msg = fmt.Sprintf("%s: %s", e.Msg, msg)
	}
	return out
}

func New(code int, msg string) *Error {
	return &Error{Code: code, Msg: msg}
}