errors.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package fserr
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "io"
  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. ErrDataCode uint32 = 0x00000007
  15. ErrCustomCode uint32 = 0x00001000
  16. )
  17. var codeMap = map[uint32]string{
  18. ErrUnknownCode: "未知错误",
  19. ErrParamCode: "参数错误",
  20. ErrConflictCode: "资源冲突",
  21. ErrNotExistCode: "资源不存在",
  22. ErrTypeTransferCode: "类型转换错误",
  23. ErrAuthCode: "鉴权错误",
  24. ErrDataCode: "数据异常",
  25. ErrCustomCode: "自定义错误",
  26. }
  27. func ExplainCode(code uint32) string {
  28. explain, ok := codeMap[code]
  29. if !ok {
  30. return codeMap[ErrUnknownCode]
  31. }
  32. return explain
  33. }
  34. func TransferFromError(err error) *Error {
  35. transferErr, ok := err.(*Error)
  36. if ok {
  37. return transferErr
  38. }
  39. return NewUnknownError(err.Error())
  40. }
  41. func NewError(code uint32, message string) *Error {
  42. return newError(code, message)
  43. }
  44. func NewUnknownError(message string) *Error {
  45. return newError(ErrUnknownCode, message)
  46. }
  47. func NewParamError(message string) *Error {
  48. return newError(ErrParamCode, message)
  49. }
  50. func NewConflictError(message string) *Error {
  51. return newError(ErrConflictCode, message)
  52. }
  53. func NewNotExistError(message string) *Error {
  54. return newError(ErrNotExistCode, message)
  55. }
  56. func NewTypeTransferError(message string) *Error {
  57. return newError(ErrTypeTransferCode, message)
  58. }
  59. func NewAuthError(message string) *Error {
  60. return newError(ErrAuthCode, message)
  61. }
  62. func NewDataError(message string) *Error {
  63. return newError(ErrDataCode, message)
  64. }
  65. func NewCustomError(message string) *Error {
  66. return newError(ErrCustomCode, message)
  67. }
  68. type Error struct {
  69. code uint32
  70. message string
  71. StackMessage string
  72. }
  73. func (err *Error) Code() uint32 {
  74. return err.code
  75. }
  76. func (err *Error) Error() string {
  77. return err.message
  78. }
  79. func (err *Error) WithStack() error {
  80. return &Error{
  81. code: err.code,
  82. message: err.message,
  83. StackMessage: formStackMessage(err.message),
  84. }
  85. }
  86. func (err *Error) Is(target error) bool {
  87. transferErr, ok := target.(*Error)
  88. if !ok {
  89. return false
  90. }
  91. return err.message == transferErr.message && err.code == transferErr.code
  92. }
  93. func (err *Error) Format(s fmt.State, verb rune) {
  94. switch verb {
  95. case 'v':
  96. if s.Flag('+') {
  97. io.WriteString(s, err.StackMessage)
  98. return
  99. }
  100. fallthrough
  101. case 's':
  102. io.WriteString(s, err.message)
  103. case 'q':
  104. fmt.Fprintf(s, "%q", err.message)
  105. }
  106. }
  107. func newError(code uint32, message string) *Error {
  108. _, ok := codeMap[code]
  109. if !ok {
  110. code = ErrUnknownCode
  111. }
  112. return &Error{
  113. code: code,
  114. message: message,
  115. StackMessage: formStackMessage(message),
  116. }
  117. }
  118. func formStackMessage(message string) string {
  119. return fmt.Sprintf("\n%+v\n", errors.New(message))
  120. }