1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

fix #1531 and introduce the 'Configuration.ResetOnFireErrorCode' (read HISTORY.md)

Former-commit-id: 84f1e894378a6dfd94e0bf057f4037e35aee0c4f
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-06-08 05:16:55 +03:00
parent 34d0d98130
commit 7bb2223226
20 changed files with 333 additions and 260 deletions

View File

@@ -984,109 +984,11 @@ func (api *APIBuilder) OnErrorCode(statusCode int, handlers ...context.Handler)
return
}
// ClientErrorCodes holds the 4xx Client errors.
var (
ClientErrorCodes = []int{
http.StatusBadRequest,
http.StatusUnauthorized,
http.StatusPaymentRequired,
http.StatusForbidden,
http.StatusNotFound,
http.StatusMethodNotAllowed,
http.StatusNotAcceptable,
http.StatusProxyAuthRequired,
http.StatusRequestTimeout,
http.StatusConflict,
http.StatusGone,
http.StatusLengthRequired,
http.StatusPreconditionFailed,
http.StatusRequestEntityTooLarge,
http.StatusRequestURITooLong,
http.StatusUnsupportedMediaType,
http.StatusRequestedRangeNotSatisfiable,
http.StatusExpectationFailed,
http.StatusTeapot,
http.StatusMisdirectedRequest,
http.StatusUnprocessableEntity,
http.StatusLocked,
http.StatusFailedDependency,
http.StatusTooEarly,
http.StatusUpgradeRequired,
http.StatusPreconditionRequired,
http.StatusTooManyRequests,
http.StatusRequestHeaderFieldsTooLarge,
http.StatusUnavailableForLegalReasons,
// Unofficial.
StatusPageExpired,
StatusBlockedByWindowsParentalControls,
StatusInvalidToken,
StatusTokenRequired,
}
// ServerErrorCodes holds the 5xx Server errors.
ServerErrorCodes = []int{
http.StatusInternalServerError,
http.StatusNotImplemented,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout,
http.StatusHTTPVersionNotSupported,
http.StatusVariantAlsoNegotiates,
http.StatusInsufficientStorage,
http.StatusLoopDetected,
http.StatusNotExtended,
http.StatusNetworkAuthenticationRequired,
// Unofficial.
StatusBandwidthLimitExceeded,
StatusInvalidSSLCertificate,
StatusSiteOverloaded,
StatusSiteFrozen,
StatusNetworkReadTimeout,
}
)
// Unofficial status error codes.
const (
// 4xx
StatusPageExpired = 419
StatusBlockedByWindowsParentalControls = 450
StatusInvalidToken = 498
StatusTokenRequired = 499
// 5xx
StatusBandwidthLimitExceeded = 509
StatusInvalidSSLCertificate = 526
StatusSiteOverloaded = 529
StatusSiteFrozen = 530
StatusNetworkReadTimeout = 598
)
var unofficialStatusText = map[int]string{
StatusPageExpired: "Page Expired",
StatusBlockedByWindowsParentalControls: "Blocked by Windows Parental Controls",
StatusInvalidToken: "Invalid Token",
StatusTokenRequired: "Token Required",
StatusBandwidthLimitExceeded: "Bandwidth Limit Exceeded",
StatusInvalidSSLCertificate: "Invalid SSL Certificate",
StatusSiteOverloaded: "Site is overloaded",
StatusSiteFrozen: "Site is frozen",
StatusNetworkReadTimeout: "Network read timeout error",
}
// StatusText returns a text for the HTTP status code. It returns the empty
// string if the code is unknown.
func StatusText(code int) string {
text := http.StatusText(code)
if text == "" {
text = unofficialStatusText[code]
}
return text
}
// OnAnyErrorCode registers a handlers chain for all error codes
// (4xxx and 5xxx, change the `ClientErrorCodes` and `ServerErrorCodes` variables to modify those)
// (4xxx and 5xxx, change the `context.ClientErrorCodes` and `context.ServerErrorCodes` variables to modify those)
// Look `OnErrorCode` too.
func (api *APIBuilder) OnAnyErrorCode(handlers ...context.Handler) (routes []*Route) {
for _, statusCode := range append(ClientErrorCodes, ServerErrorCodes...) {
for _, statusCode := range context.ClientAndServerErrorCodes {
routes = append(routes, api.OnErrorCode(statusCode, handlers...)...)
}