errors.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package fserr
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. )
  6. const (
  7. ErrUnknownCode uint32 = 0x00000001
  8. ErrParamCode uint32 = 0x00000002
  9. ErrConflictCode uint32 = 0x00000003
  10. ErrNotExistCode uint32 = 0x00000004
  11. ErrTypeTransferCode uint32 = 0x00000005
  12. ErrAuthCode uint32 = 0x00000006
  13. ErrCustomCode uint32 = 0x00001000
  14. )
  15. var codeMap = map[uint32]string{
  16. ErrUnknownCode: "未知错误",
  17. ErrParamCode: "参数错误",
  18. ErrConflictCode: "资源冲突",
  19. ErrNotExistCode: "资源不存在",
  20. ErrTypeTransferCode: "类型转换错误",
  21. ErrAuthCode: "鉴权错误",
  22. ErrCustomCode: "自定义错误",
  23. }
  24. func NewUnknownError(message string) error {
  25. return newError(ErrUnknownCode, message)
  26. }
  27. func NewParamError(message string) error {
  28. return newError(ErrParamCode, message)
  29. }
  30. func NewConflictError(message string) error {
  31. return newError(ErrConflictCode, message)
  32. }
  33. func NewNotExistError(message string) error {
  34. return newError(ErrNotExistCode, message)
  35. }
  36. func NewTypeTransferError(message string) error {
  37. return newError(ErrTypeTransferCode, message)
  38. }
  39. func NewAuthError(message string) error {
  40. return newError(ErrAuthCode, message)
  41. }
  42. func NewCustomError(message string) error {
  43. return newError(ErrCustomCode, message)
  44. }
  45. type Error struct {
  46. code uint32
  47. error
  48. }
  49. func (err Error) Error() string {
  50. errMsg := fmt.Sprintf("错误代码: 0x%08x\n代码信息: %s\n%+v\n", err.code, codeMap[err.code], err.error)
  51. return errMsg
  52. }
  53. func newError(code uint32, message string) *Error {
  54. return &Error{
  55. code: code,
  56. error: errors.New(message),
  57. }
  58. }