2022-06-14 03:50:11 -05:00
|
|
|
package errutil
|
|
|
|
|
2024-05-20 10:11:37 -05:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
2022-06-14 03:50:11 -05:00
|
|
|
|
|
|
|
const (
|
|
|
|
// StatusUnknown implies an error that should be updated to contain
|
|
|
|
// an accurate status code, as none has been provided.
|
|
|
|
// HTTP status code 500.
|
|
|
|
StatusUnknown CoreStatus = ""
|
|
|
|
// StatusUnauthorized means that the server does not recognize the
|
|
|
|
// client's authentication, either because it has not been provided
|
|
|
|
// or is invalid for the operation.
|
|
|
|
// HTTP status code 401.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusUnauthorized CoreStatus = CoreStatus(metav1.StatusReasonUnauthorized)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusForbidden means that the server refuses to perform the
|
|
|
|
// requested action for the authenticated uer.
|
|
|
|
// HTTP status code 403.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusForbidden CoreStatus = CoreStatus(metav1.StatusReasonForbidden)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusNotFound means that the server does not have any
|
|
|
|
// corresponding document to return to the request.
|
|
|
|
// HTTP status code 404.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusNotFound CoreStatus = CoreStatus(metav1.StatusReasonNotFound)
|
2023-11-30 08:49:27 -06:00
|
|
|
// StatusUnprocessableEntity means that the server understands the request,
|
|
|
|
// the content type and the syntax but it was unable to process the
|
|
|
|
// contained instructions.
|
|
|
|
// HTTP status code 422.
|
|
|
|
StatusUnprocessableEntity CoreStatus = "Unprocessable Entity"
|
|
|
|
// StatusConflict means that the server cannot fulfill the request
|
|
|
|
// there is a conflict in the current state of a resource
|
|
|
|
// HTTP status code 409.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusConflict CoreStatus = CoreStatus(metav1.StatusReasonConflict)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusTooManyRequests means that the client is rate limited
|
|
|
|
// by the server and should back-off before trying again.
|
|
|
|
// HTTP status code 429.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusTooManyRequests CoreStatus = CoreStatus(metav1.StatusReasonTooManyRequests)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusBadRequest means that the server was unable to parse the
|
|
|
|
// parameters or payload for the request.
|
|
|
|
// HTTP status code 400.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusBadRequest CoreStatus = CoreStatus(metav1.StatusReasonBadRequest)
|
2023-10-10 05:28:39 -05:00
|
|
|
// StatusClientClosedRequest means that a client closes the connection
|
|
|
|
// while the server is processing the request.
|
|
|
|
//
|
|
|
|
// This is a non-standard HTTP status code introduced by nginx, see
|
|
|
|
// https://httpstatus.in/499/ for more information.
|
|
|
|
// HTTP status code 499.
|
|
|
|
StatusClientClosedRequest CoreStatus = "Client closed request"
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusValidationFailed means that the server was able to parse
|
|
|
|
// the payload for the request but it failed one or more validation
|
|
|
|
// checks.
|
|
|
|
// HTTP status code 400.
|
|
|
|
StatusValidationFailed CoreStatus = "Validation failed"
|
|
|
|
// StatusInternal means that the server acknowledges that there's
|
|
|
|
// an error, but that there is nothing the client can do to fix it.
|
|
|
|
// HTTP status code 500.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusInternal CoreStatus = CoreStatus(metav1.StatusReasonInternalError)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusTimeout means that the server did not complete the request
|
|
|
|
// within the required time and aborted the action.
|
|
|
|
// HTTP status code 504.
|
2024-05-20 10:11:37 -05:00
|
|
|
StatusTimeout CoreStatus = CoreStatus(metav1.StatusReasonTimeout)
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusNotImplemented means that the server does not support the
|
|
|
|
// requested action. Typically used during development of new
|
|
|
|
// features.
|
|
|
|
// HTTP status code 501.
|
|
|
|
StatusNotImplemented CoreStatus = "Not implemented"
|
2023-09-11 05:13:13 -05:00
|
|
|
// StatusBadGateway means that the server, while acting as a proxy,
|
|
|
|
// received an invalid response from the downstream server.
|
|
|
|
// HTTP status code 502.
|
|
|
|
StatusBadGateway CoreStatus = "Bad gateway"
|
|
|
|
// StatusGatewayTimeout means that the server, while acting as a proxy,
|
|
|
|
// did not receive a timely response from a downstream server it needed
|
|
|
|
// to access in order to complete the request.
|
|
|
|
// HTTP status code 504.
|
|
|
|
StatusGatewayTimeout CoreStatus = "Gateway timeout"
|
2022-06-14 03:50:11 -05:00
|
|
|
)
|
|
|
|
|
2023-10-10 05:28:39 -05:00
|
|
|
// HTTPStatusClientClosedRequest A non-standard status code introduced by nginx
|
|
|
|
// for the case when a client closes the connection while nginx is processing
|
|
|
|
// the request. See https://httpstatus.in/499/ for more information.
|
|
|
|
const HTTPStatusClientClosedRequest = 499
|
|
|
|
|
2022-06-14 03:50:11 -05:00
|
|
|
// StatusReason allows for wrapping of CoreStatus.
|
|
|
|
type StatusReason interface {
|
|
|
|
Status() CoreStatus
|
|
|
|
}
|
|
|
|
|
2024-05-20 10:11:37 -05:00
|
|
|
type CoreStatus metav1.StatusReason
|
2022-06-14 03:50:11 -05:00
|
|
|
|
|
|
|
// Status implements the StatusReason interface.
|
|
|
|
func (s CoreStatus) Status() CoreStatus {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPStatus converts the CoreStatus to an HTTP status code.
|
|
|
|
func (s CoreStatus) HTTPStatus() int {
|
|
|
|
switch s {
|
|
|
|
case StatusUnauthorized:
|
|
|
|
return http.StatusUnauthorized
|
|
|
|
case StatusForbidden:
|
|
|
|
return http.StatusForbidden
|
|
|
|
case StatusNotFound:
|
|
|
|
return http.StatusNotFound
|
2023-09-11 05:13:13 -05:00
|
|
|
case StatusTimeout, StatusGatewayTimeout:
|
2022-06-14 03:50:11 -05:00
|
|
|
return http.StatusGatewayTimeout
|
2023-11-30 08:49:27 -06:00
|
|
|
case StatusUnprocessableEntity:
|
|
|
|
return http.StatusUnprocessableEntity
|
|
|
|
case StatusConflict:
|
|
|
|
return http.StatusConflict
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusTooManyRequests:
|
|
|
|
return http.StatusTooManyRequests
|
|
|
|
case StatusBadRequest, StatusValidationFailed:
|
|
|
|
return http.StatusBadRequest
|
2023-10-10 05:28:39 -05:00
|
|
|
case StatusClientClosedRequest:
|
|
|
|
return HTTPStatusClientClosedRequest
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusNotImplemented:
|
|
|
|
return http.StatusNotImplemented
|
2023-09-11 05:13:13 -05:00
|
|
|
case StatusBadGateway:
|
|
|
|
return http.StatusBadGateway
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusUnknown, StatusInternal:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
default:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogLevel returns the default LogLevel for the CoreStatus.
|
|
|
|
func (s CoreStatus) LogLevel() LogLevel {
|
|
|
|
switch s {
|
|
|
|
case StatusUnauthorized:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusForbidden:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusNotFound:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusTimeout:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2023-11-30 08:49:27 -06:00
|
|
|
case StatusUnprocessableEntity:
|
|
|
|
return LevelInfo
|
|
|
|
case StatusConflict:
|
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusTooManyRequests:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusBadRequest:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusValidationFailed:
|
2023-05-05 09:45:48 -05:00
|
|
|
return LevelInfo
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusNotImplemented:
|
2022-09-15 07:08:59 -05:00
|
|
|
return LevelDebug
|
2022-06-14 03:50:11 -05:00
|
|
|
case StatusUnknown, StatusInternal:
|
|
|
|
return LevelError
|
|
|
|
default:
|
|
|
|
return LevelUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:14:28 -05:00
|
|
|
func (s CoreStatus) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2022-06-14 03:50:11 -05:00
|
|
|
// ProxyStatus implies that an error originated from the data source
|
|
|
|
// proxy.
|
|
|
|
type ProxyStatus CoreStatus
|
|
|
|
|
|
|
|
// Status implements the StatusReason interface.
|
|
|
|
func (s ProxyStatus) Status() CoreStatus {
|
|
|
|
return CoreStatus(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PluginStatus implies that an error originated from a plugin.
|
|
|
|
type PluginStatus CoreStatus
|
|
|
|
|
|
|
|
// Status implements the StatusReason interface.
|
|
|
|
func (s PluginStatus) Status() CoreStatus {
|
|
|
|
return CoreStatus(s)
|
|
|
|
}
|