| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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 NewFromError(err error) error {
- _, ok := err.(*Error)
- if ok {
- return err
- }
- return NewUnknownError(err.Error())
- }
- 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),
- }
- }
|