| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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 ExplainCode(code uint32) string {
- explain, ok := codeMap[code]
- if !ok {
- return codeMap[ErrUnknownCode]
- }
- return explain
- }
- func TransferFromError(err error) *Error {
- transferedErr, ok := err.(*Error)
- if ok {
- return transferedErr
- }
- return &Error{
- code: ErrUnknownCode,
- error: err,
- }
- }
- 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) Code() uint32 {
- return err.code
- }
- 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 (err *Error) WithStack() error {
- return errors.WithStack(err)
- }
- func newError(code uint32, message string) *Error {
- return &Error{
- code: code,
- error: errors.New(message),
- }
- }
|