code.go 3.4 KB

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