1
0
mirror of https://github.com/rancher/norman.git synced 2025-04-30 20:33:43 +00:00
norman/httperror/error.go

130 lines
3.3 KiB
Go
Raw Permalink Normal View History

2017-10-16 02:23:15 +00:00
package httperror
import (
"fmt"
)
var (
Unauthorized = ErrorCode{"Unauthorized", 401}
PermissionDenied = ErrorCode{"PermissionDenied", 403}
NotFound = ErrorCode{"NotFound", 404}
MethodNotAllowed = ErrorCode{"MethodNotAllow", 405}
2018-05-22 05:36:46 +00:00
Conflict = ErrorCode{"Conflict", 409}
2017-11-21 20:46:30 +00:00
InvalidDateFormat = ErrorCode{"InvalidDateFormat", 422}
InvalidFormat = ErrorCode{"InvalidFormat", 422}
InvalidReference = ErrorCode{"InvalidReference", 422}
NotNullable = ErrorCode{"NotNullable", 422}
NotUnique = ErrorCode{"NotUnique", 422}
MinLimitExceeded = ErrorCode{"MinLimitExceeded", 422}
MaxLimitExceeded = ErrorCode{"MaxLimitExceeded", 422}
MinLengthExceeded = ErrorCode{"MinLengthExceeded", 422}
MaxLengthExceeded = ErrorCode{"MaxLengthExceeded", 422}
InvalidOption = ErrorCode{"InvalidOption", 422}
InvalidCharacters = ErrorCode{"InvalidCharacters", 422}
MissingRequired = ErrorCode{"MissingRequired", 422}
InvalidCSRFToken = ErrorCode{"InvalidCSRFToken", 422}
InvalidAction = ErrorCode{"InvalidAction", 422}
InvalidBodyContent = ErrorCode{"InvalidBodyContent", 422}
InvalidType = ErrorCode{"InvalidType", 422}
ActionNotAvailable = ErrorCode{"ActionNotAvailable", 404}
InvalidState = ErrorCode{"InvalidState", 422}
2017-11-21 20:46:30 +00:00
ServerError = ErrorCode{"ServerError", 500}
2018-02-09 20:31:12 +00:00
ClusterUnavailable = ErrorCode{"ClusterUnavailable", 503}
2017-10-16 02:23:15 +00:00
)
2017-11-11 04:44:02 +00:00
type ErrorCode struct {
Code string
2018-01-20 04:29:07 +00:00
Status int
2017-11-11 04:44:02 +00:00
}
func (e ErrorCode) String() string {
return fmt.Sprintf("%s %d", e.Code, e.Status)
2017-11-11 04:44:02 +00:00
}
2017-10-16 02:23:15 +00:00
type APIError struct {
Code ErrorCode
Message string
2017-10-16 02:23:15 +00:00
Cause error
FieldName string
2017-10-16 02:23:15 +00:00
}
func NewAPIErrorLong(status int, code, message string) error {
return NewAPIError(ErrorCode{
Code: code,
2018-01-20 04:29:07 +00:00
Status: status,
}, message)
}
2017-10-16 02:23:15 +00:00
func NewAPIError(code ErrorCode, message string) error {
return &APIError{
Code: code,
Message: message,
2017-10-16 02:23:15 +00:00
}
}
func NewFieldAPIError(code ErrorCode, fieldName, message string) error {
return &APIError{
Code: code,
Message: message,
FieldName: fieldName,
2017-10-16 02:23:15 +00:00
}
}
// WrapFieldAPIError will cause the API framework to log the underlying err before returning the APIError as a response.
// err WILL NOT be in the API response
2017-10-16 02:23:15 +00:00
func WrapFieldAPIError(err error, code ErrorCode, fieldName, message string) error {
return &APIError{
2017-11-11 04:44:02 +00:00
Cause: err,
Code: code,
Message: message,
FieldName: fieldName,
2017-10-16 02:23:15 +00:00
}
}
// WrapAPIError will cause the API framework to log the underlying err before returning the APIError as a response.
// err WILL NOT be in the API response
2017-10-16 02:23:15 +00:00
func WrapAPIError(err error, code ErrorCode, message string) error {
return &APIError{
Code: code,
Message: message,
2017-10-16 02:23:15 +00:00
Cause: err,
}
}
func (a *APIError) Error() string {
if a.FieldName != "" {
return fmt.Sprintf("%s=%s: %s", a.FieldName, a.Code, a.Message)
2017-11-11 04:44:02 +00:00
}
return fmt.Sprintf("%s: %s", a.Code, a.Message)
2017-10-16 02:23:15 +00:00
}
func IsAPIError(err error) bool {
_, ok := err.(*APIError)
return ok
}
func IsNotFound(err error) bool {
if apiError, ok := err.(*APIError); ok {
return apiError.Code.Status == 404
}
return false
}
func IsConflict(err error) bool {
if apiError, ok := err.(*APIError); ok {
return apiError.Code.Status == 409
}
return false
}
2019-11-12 00:43:25 +00:00
func IsForbidden(err error) bool {
if apiError, ok := err.(*APIError); ok {
return apiError.Code.Status == 403
}
return false
}