2023-10-16 13:56:00 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// StatusCodesToError maps HTTP status codes to corresponding error types.
|
2023-10-16 13:56:00 +00:00
|
|
|
func StatusCodesToError(statusCode int) error {
|
|
|
|
var err error
|
|
|
|
switch statusCode {
|
|
|
|
case 403:
|
|
|
|
err = AccessDeniedError{}
|
|
|
|
case 404:
|
|
|
|
err = NotFoundError{}
|
|
|
|
case 412:
|
|
|
|
err = PreconditionFailedError{}
|
|
|
|
case 421:
|
|
|
|
err = RateLimitReachedError{}
|
|
|
|
case 424:
|
|
|
|
err = InvalidParametersError{}
|
|
|
|
case 500:
|
|
|
|
err = InternalServerError{}
|
|
|
|
case 502:
|
|
|
|
err = BadGatewayError{}
|
|
|
|
case 503:
|
|
|
|
err = ServiceUnavailableError{}
|
|
|
|
default:
|
2023-11-06 14:56:36 +00:00
|
|
|
err = fmt.Errorf("unhandled status code: %d", statusCode)
|
2023-10-16 13:56:00 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// AccessDeniedError represents an "Access Denied" error.
|
|
|
|
type AccessDeniedError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ AccessDeniedError) Error() string {
|
|
|
|
return "access denied"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// NotFoundError represents a "Not Found" error.
|
|
|
|
type NotFoundError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ NotFoundError) Error() string {
|
|
|
|
return "not found"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// PreconditionFailedError represents a "Precondition Failed" error.
|
|
|
|
type PreconditionFailedError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ PreconditionFailedError) Error() string {
|
|
|
|
return "precondition failed"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// RateLimitReachedError represents a "Rate Limit Reached" error.
|
|
|
|
type RateLimitReachedError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ RateLimitReachedError) Error() string {
|
|
|
|
return "rate limit reached"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// InvalidParametersError represents an "Invalid Parameters" error.
|
|
|
|
type InvalidParametersError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ InvalidParametersError) Error() string {
|
2023-11-06 14:56:36 +00:00
|
|
|
return "invalid parameters"
|
2023-10-16 13:56:00 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// InternalServerError represents an "Internal Server Error" error.
|
|
|
|
type InternalServerError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ InternalServerError) Error() string {
|
|
|
|
return "internal server error"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// BadGatewayError represents a "Bad Gateway" error.
|
|
|
|
type BadGatewayError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ BadGatewayError) Error() string {
|
|
|
|
return "bad gateway"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// ServiceUnavailableError represents a "Service Unavailable" error.
|
|
|
|
type ServiceUnavailableError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ ServiceUnavailableError) Error() string {
|
|
|
|
return "service unavailable"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// UnknownError represents an "Unknown" error.
|
|
|
|
type UnknownError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ UnknownError) Error() string {
|
|
|
|
return "unknown error"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
|
|
|
|
type OriginConnectionTimeOutError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ OriginConnectionTimeOutError) Error() string {
|
|
|
|
return "origin connection time-out"
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
|
|
|
|
type SSLHandshakeFailedError struct{}
|
2023-10-16 13:56:00 +00:00
|
|
|
|
|
|
|
func (_ SSLHandshakeFailedError) Error() string {
|
|
|
|
return "ssl handshake failed"
|
|
|
|
}
|