This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/utils/error.go
SoXX 8c662f78fd docs: added more & unified documentation format
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-15 14:41:16 +01:00

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"
}