code.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package fserr
  2. import (
  3. "net/http"
  4. "sync/atomic"
  5. )
  6. type codeType interface {
  7. int8 | int16 | int32 | int64 | int |
  8. uint8 | uint16 | uint32 | uint64 | uint
  9. }
  10. type atomicServiceCode struct {
  11. atomic.Int64
  12. }
  13. func (a *atomicServiceCode) Load() int {
  14. return int(a.Int64.Load())
  15. }
  16. func (a *atomicServiceCode) Store(v int64) {
  17. a.Int64.Store(v)
  18. }
  19. var serviceCode atomicServiceCode
  20. const (
  21. ErrBasic = iota + 1
  22. ErrDb
  23. ErrParam
  24. ErrRetry
  25. ErrServiceInvoke
  26. )
  27. // ErrCode 产生error所包含的错误码信息
  28. // 支持http错误码、业务错误码
  29. // 可通过下方快捷函数快速创建指定http码的错误码
  30. type ErrCode struct {
  31. // HttpCode 该错误码建议的HTTP响应码
  32. HttpCode int `json:"httpCode,omitempty"`
  33. // BusinessCode 该错误码对应的业务码
  34. // +optional
  35. BusinessCode int `json:"businessCode,omitempty"`
  36. // Message 该错误码对应的错误信息
  37. // +optional
  38. Message string `json:"message,omitempty"`
  39. }
  40. // SetDefault 设置默认错误码
  41. // 当错误码匹配失败时,提供的备选方案,已内置默认错误码,
  42. // 它的HTTP码为200,业务码和信息均为零值
  43. func SetDefault(httpCode, businessCode int, message string) {
  44. defaultErrCode = ErrCode{httpCode, serviceCode.Load() + businessCode, message}
  45. }
  46. // NewCode 创建指定信息的错误码
  47. func NewCode(httpCode, businessCode int, message string) ErrCode {
  48. code := ErrCode{httpCode, serviceCode.Load() + businessCode, message}
  49. register(code)
  50. return code
  51. }
  52. // === 以下均为见名知意的业务码构建方式 ===
  53. func NewOK(businessCode int, message string) ErrCode {
  54. return NewCode(http.StatusOK, businessCode, message)
  55. }
  56. func NewNotFound(businessCode int, message string) ErrCode {
  57. return NewCode(http.StatusNotFound, businessCode, message)
  58. }
  59. func NewAlreadyExists(businessCode int, message string) ErrCode {
  60. return NewCode(http.StatusConflict, businessCode, message)
  61. }
  62. func NewGenerateNameConflict(businessCode int, message string) ErrCode {
  63. return NewCode(http.StatusConflict, businessCode, message)
  64. }
  65. func NewUnauthorized(businessCode int, message string) ErrCode {
  66. return NewCode(http.StatusUnauthorized, businessCode, message)
  67. }
  68. func NewForbidden(businessCode int, message string) ErrCode {
  69. return NewCode(http.StatusForbidden, businessCode, message)
  70. }
  71. func NewConflict(businessCode int, message string) ErrCode {
  72. return NewCode(http.StatusConflict, businessCode, message)
  73. }
  74. func NewGone(businessCode int, message string) ErrCode {
  75. return NewCode(http.StatusGone, businessCode, message)
  76. }
  77. func NewBadRequest(businessCode int, message string) ErrCode {
  78. return NewCode(http.StatusBadRequest, businessCode, message)
  79. }
  80. func NewTooManyRequests(businessCode int, message string) ErrCode {
  81. return NewCode(http.StatusTooManyRequests, businessCode, message)
  82. }
  83. func NewServiceUnavailable(businessCode int, message string) ErrCode {
  84. return NewCode(http.StatusServiceUnavailable, businessCode, message)
  85. }
  86. func NewMethodNotSupported(businessCode int, message string) ErrCode {
  87. return NewCode(http.StatusMethodNotAllowed, businessCode, message)
  88. }
  89. func NewInternalError(businessCode int, message string) ErrCode {
  90. return NewCode(http.StatusInternalServerError, businessCode, message)
  91. }
  92. func NewTimeoutError(businessCode int, message string) ErrCode {
  93. return NewCode(http.StatusGatewayTimeout, businessCode, message)
  94. }
  95. func NewTooManyRequestsError(businessCode int, message string) ErrCode {
  96. return NewCode(http.StatusTooManyRequests, businessCode, message)
  97. }
  98. func NewRequestEntityTooLargeError(businessCode int, message string) ErrCode {
  99. return NewCode(http.StatusRequestEntityTooLarge, businessCode, message)
  100. }