124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package utils
|
|
|
|
import "fmt"
|
|
|
|
// StatusCodesToError maps HTTP status codes to corresponding error types.
|
|
//
|
|
// Parameters:
|
|
// - statusCode: The HTTP status code to be mapped to an error type.
|
|
//
|
|
// Returns:
|
|
// - error: An error representing the mapped HTTP status code.
|
|
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:
|
|
err = fmt.Errorf("unhandled status code: %d", statusCode)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// AccessDeniedError represents an "Access Denied" error.
|
|
type AccessDeniedError struct{}
|
|
|
|
// Error returns the error message for AccessDeniedError.
|
|
func (_ AccessDeniedError) Error() string {
|
|
return "access denied"
|
|
}
|
|
|
|
// NotFoundError represents a "Not Found" error.
|
|
type NotFoundError struct{}
|
|
|
|
// Error returns the error message for NotFoundError.
|
|
func (_ NotFoundError) Error() string {
|
|
return "not found"
|
|
}
|
|
|
|
// PreconditionFailedError represents a "Precondition Failed" error.
|
|
type PreconditionFailedError struct{}
|
|
|
|
// Error returns the error message for PreconditionFailedError.
|
|
func (_ PreconditionFailedError) Error() string {
|
|
return "precondition failed"
|
|
}
|
|
|
|
// RateLimitReachedError represents a "Rate Limit Reached" error.
|
|
type RateLimitReachedError struct{}
|
|
|
|
// Error returns the error message for RateLimitReachedError.
|
|
func (_ RateLimitReachedError) Error() string {
|
|
return "rate limit reached"
|
|
}
|
|
|
|
// InvalidParametersError represents an "Invalid Parameters" error.
|
|
type InvalidParametersError struct{}
|
|
|
|
// Error returns the error message for InvalidParametersError.
|
|
func (_ InvalidParametersError) Error() string {
|
|
return "invalid parameters"
|
|
}
|
|
|
|
// InternalServerError represents an "Internal Server Error" error.
|
|
type InternalServerError struct{}
|
|
|
|
// Error returns the error message for InternalServerError.
|
|
func (_ InternalServerError) Error() string {
|
|
return "internal server error"
|
|
}
|
|
|
|
// BadGatewayError represents a "Bad Gateway" error.
|
|
type BadGatewayError struct{}
|
|
|
|
// Error returns the error message for BadGatewayError.
|
|
func (_ BadGatewayError) Error() string {
|
|
return "bad gateway"
|
|
}
|
|
|
|
// ServiceUnavailableError represents a "Service Unavailable" error.
|
|
type ServiceUnavailableError struct{}
|
|
|
|
// Error returns the error message for ServiceUnavailableError.
|
|
func (_ ServiceUnavailableError) Error() string {
|
|
return "service unavailable"
|
|
}
|
|
|
|
// UnknownError represents an "Unknown" error.
|
|
type UnknownError struct{}
|
|
|
|
// Error returns the error message for UnknownError.
|
|
func (_ UnknownError) Error() string {
|
|
return "unknown error"
|
|
}
|
|
|
|
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
|
|
type OriginConnectionTimeOutError struct{}
|
|
|
|
// Error returns the error message for OriginConnectionTimeOutError.
|
|
func (_ OriginConnectionTimeOutError) Error() string {
|
|
return "origin connection time-out"
|
|
}
|
|
|
|
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
|
|
type SSLHandshakeFailedError struct{}
|
|
|
|
// Error returns the error message for SSLHandshakeFailedError.
|
|
func (_ SSLHandshakeFailedError) Error() string {
|
|
return "ssl handshake failed"
|
|
}
|