errors.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package fserr
  2. import (
  3. "errors"
  4. "fmt"
  5. pkgerrors "github.com/pkg/errors"
  6. )
  7. const (
  8. ErrUnknownCode uint32 = 0x00000001
  9. ErrParamCode uint32 = 0x00000002
  10. ErrConflictCode uint32 = 0x00000003
  11. ErrNotExistCode uint32 = 0x00000004
  12. ErrTypeTransferCode uint32 = 0x00000005
  13. ErrAuthCode uint32 = 0x00000006
  14. ErrCustomCode uint32 = 0x00001000
  15. )
  16. var codeMap = map[uint32]string{
  17. ErrUnknownCode: "未知错误",
  18. ErrParamCode: "参数错误",
  19. ErrConflictCode: "资源冲突",
  20. ErrNotExistCode: "资源不存在",
  21. ErrTypeTransferCode: "类型转换错误",
  22. ErrAuthCode: "鉴权错误",
  23. ErrCustomCode: "自定义错误",
  24. }
  25. func ExplainCode(code uint32) string {
  26. explain, ok := codeMap[code]
  27. if !ok {
  28. return codeMap[ErrUnknownCode]
  29. }
  30. return explain
  31. }
  32. func TransferFromError(err error) *Error {
  33. transferErr, ok := err.(*Error)
  34. if ok {
  35. return transferErr
  36. }
  37. return &Error{
  38. code: ErrUnknownCode,
  39. message: err.Error(),
  40. error: pkgerrors.New(err.Error()),
  41. }
  42. }
  43. func NewUnknownError(message string) *Error {
  44. return newError(ErrUnknownCode, message)
  45. }
  46. func NewParamError(message string) *Error {
  47. return newError(ErrParamCode, message)
  48. }
  49. func NewConflictError(message string) *Error {
  50. return newError(ErrConflictCode, message)
  51. }
  52. func NewNotExistError(message string) *Error {
  53. return newError(ErrNotExistCode, message)
  54. }
  55. func NewTypeTransferError(message string) *Error {
  56. return newError(ErrTypeTransferCode, message)
  57. }
  58. func NewAuthError(message string) *Error {
  59. return newError(ErrAuthCode, message)
  60. }
  61. func NewCustomError(message string) *Error {
  62. return newError(ErrCustomCode, message)
  63. }
  64. type Error struct {
  65. code uint32
  66. message string
  67. error
  68. }
  69. func (err *Error) Code() uint32 {
  70. return err.code
  71. }
  72. func (err *Error) Error() string {
  73. errMsg := fmt.Sprintf("\n错误代码: 0x%08x\n代码信息: %s\n%+v\n", err.code, codeMap[err.code], err.error)
  74. return errMsg
  75. }
  76. func (err *Error) WithStack() error {
  77. return &Error{
  78. code: err.code,
  79. message: err.message,
  80. error: pkgerrors.New(err.message),
  81. }
  82. }
  83. func (err *Error) Is(target error) bool {
  84. transferErr, ok := target.(*Error)
  85. if !ok {
  86. return false
  87. }
  88. return err.message == transferErr.message && err.code == transferErr.code
  89. }
  90. func newError(code uint32, message string) *Error {
  91. return &Error{
  92. code: code,
  93. message: message,
  94. error: errors.New(message),
  95. }
  96. }