package fserr import ( "fmt" "github.com/pkg/errors" ) const ( ErrUnknownCode uint32 = 0x00000001 ErrParamCode uint32 = 0x00000002 ErrConflictCode uint32 = 0x00000003 ErrNotExistCode uint32 = 0x00000004 ErrTypeTransferCode uint32 = 0x00000005 ErrAuthCode uint32 = 0x00000006 ErrCustomCode uint32 = 0x00001000 ) var codeMap = map[uint32]string{ ErrUnknownCode: "未知错误", ErrParamCode: "参数错误", ErrConflictCode: "资源冲突", ErrNotExistCode: "资源不存在", ErrTypeTransferCode: "类型转换错误", ErrAuthCode: "鉴权错误", ErrCustomCode: "自定义错误", } func NewUnknownError(message string) error { return newError(ErrUnknownCode, message) } func NewParamError(message string) error { return newError(ErrParamCode, message) } func NewConflictError(message string) error { return newError(ErrConflictCode, message) } func NewNotExistError(message string) error { return newError(ErrNotExistCode, message) } func NewTypeTransferError(message string) error { return newError(ErrTypeTransferCode, message) } func NewAuthError(message string) error { return newError(ErrAuthCode, message) } func NewCustomError(message string) error { return newError(ErrCustomCode, message) } type Error struct { code uint32 error } func (err Error) Error() string { errMsg := fmt.Sprintf("错误代码: 0x%08x\n代码信息: %s\n%+v\n", err.code, codeMap[err.code], err.error) return errMsg } func newError(code uint32, message string) *Error { return &Error{ code: code, error: errors.New(message), } }