You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
749 B
Go

3 years ago
package nerr
import (
"fmt"
"github.com/pkg/errors"
)
3 years ago
type Error struct {
Code uint32 `json:"code"`
Msg string `json:"msg"`
}
func NewError(code uint32, msg string) error {
return &Error{
Code: code,
Msg: msg,
}
}
func NewWithMsg(msg string) error {
return NewError(ServerCommonError, msg)
}
func NewWithMsgf(format string, args ...any) error {
return NewWithMsg(fmt.Sprintf(format, args))
}
func NewWithCode(code uint32) error {
return NewError(code, MapErrMsg(code))
}
func NewWithErr(err error) error {
cause := errors.Cause(err)
if e, ok := cause.(*Error); ok {
return e
} else {
return NewWithMsg(err.Error())
}
}
3 years ago
func (e *Error) Error() string {
return fmt.Sprintf("errorCode:%d, errorMsg:%s", e.Code, e.Msg)
}