status.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package context
  2. import "net/http"
  3. // ClientErrorCodes holds the 4xx Client errors.
  4. var (
  5. ClientErrorCodes = []int{
  6. http.StatusBadRequest,
  7. http.StatusUnauthorized,
  8. http.StatusPaymentRequired,
  9. http.StatusForbidden,
  10. http.StatusNotFound,
  11. http.StatusMethodNotAllowed,
  12. http.StatusNotAcceptable,
  13. http.StatusProxyAuthRequired,
  14. http.StatusRequestTimeout,
  15. http.StatusConflict,
  16. http.StatusGone,
  17. http.StatusLengthRequired,
  18. http.StatusPreconditionFailed,
  19. http.StatusRequestEntityTooLarge,
  20. http.StatusRequestURITooLong,
  21. http.StatusUnsupportedMediaType,
  22. http.StatusRequestedRangeNotSatisfiable,
  23. http.StatusExpectationFailed,
  24. http.StatusTeapot,
  25. http.StatusMisdirectedRequest,
  26. http.StatusUnprocessableEntity,
  27. http.StatusLocked,
  28. http.StatusFailedDependency,
  29. http.StatusTooEarly,
  30. http.StatusUpgradeRequired,
  31. http.StatusPreconditionRequired,
  32. http.StatusTooManyRequests,
  33. http.StatusRequestHeaderFieldsTooLarge,
  34. http.StatusUnavailableForLegalReasons,
  35. // Unofficial.
  36. StatusPageExpired,
  37. StatusBlockedByWindowsParentalControls,
  38. StatusInvalidToken,
  39. StatusTokenRequired,
  40. }
  41. // ServerErrorCodes holds the 5xx Server errors.
  42. ServerErrorCodes = []int{
  43. http.StatusInternalServerError,
  44. http.StatusNotImplemented,
  45. http.StatusBadGateway,
  46. http.StatusServiceUnavailable,
  47. http.StatusGatewayTimeout,
  48. http.StatusHTTPVersionNotSupported,
  49. http.StatusVariantAlsoNegotiates,
  50. http.StatusInsufficientStorage,
  51. http.StatusLoopDetected,
  52. http.StatusNotExtended,
  53. http.StatusNetworkAuthenticationRequired,
  54. // Unofficial.
  55. StatusBandwidthLimitExceeded,
  56. StatusInvalidSSLCertificate,
  57. StatusSiteOverloaded,
  58. StatusSiteFrozen,
  59. StatusNetworkReadTimeout,
  60. }
  61. // ClientAndServerErrorCodes is the static list of all client and server error codes.
  62. ClientAndServerErrorCodes = append(ClientErrorCodes, ServerErrorCodes...)
  63. )
  64. // Unofficial status error codes.
  65. const (
  66. // 4xx
  67. StatusPageExpired = 419
  68. StatusBlockedByWindowsParentalControls = 450
  69. StatusInvalidToken = 498
  70. StatusTokenRequired = 499
  71. // 5xx
  72. StatusBandwidthLimitExceeded = 509
  73. StatusInvalidSSLCertificate = 526
  74. StatusSiteOverloaded = 529
  75. StatusSiteFrozen = 530
  76. StatusNetworkReadTimeout = 598
  77. )
  78. var unofficialStatusText = map[int]string{
  79. StatusPageExpired: "Page Expired",
  80. StatusBlockedByWindowsParentalControls: "Blocked by Windows Parental Controls",
  81. StatusInvalidToken: "Invalid Token",
  82. StatusTokenRequired: "Token Required",
  83. StatusBandwidthLimitExceeded: "Bandwidth Limit Exceeded",
  84. StatusInvalidSSLCertificate: "Invalid SSL Certificate",
  85. StatusSiteOverloaded: "Site is overloaded",
  86. StatusSiteFrozen: "Site is frozen",
  87. StatusNetworkReadTimeout: "Network read timeout error",
  88. }
  89. // StatusText returns a text for the HTTP status code. It returns the empty
  90. // string if the code is unknown.
  91. func StatusText(code int) string {
  92. text := http.StatusText(code)
  93. if text == "" {
  94. text = unofficialStatusText[code]
  95. }
  96. return text
  97. }
  98. // StatusCodeNotSuccessful defines if a specific "statusCode" is not
  99. // a valid status code for a successful response.
  100. // By default if the status code is lower than 400 then it is not a failure one,
  101. // otherwise it is considered as an error code.
  102. //
  103. // Read more at `iris/Configuration#DisableAutoFireStatusCode` and
  104. // `iris/core/router/Party#OnAnyErrorCode` for relative information.
  105. //
  106. // Modify this variable when your Iris server or/and client
  107. // not follows the RFC: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  108. var StatusCodeNotSuccessful = func(statusCode int) bool { return statusCode >= 400 }