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

124 lines
3.3 KiB
Go
Raw Normal View History

2023-10-16 13:56:00 +00:00
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.
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:
err = fmt.Errorf("unhandled status code: %d", statusCode)
2023-10-16 13:56:00 +00:00
}
return err
}
// AccessDeniedError represents an "Access Denied" error.
type AccessDeniedError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for AccessDeniedError.
2023-10-16 13:56:00 +00:00
func (_ AccessDeniedError) Error() string {
return "access denied"
}
// NotFoundError represents a "Not Found" error.
type NotFoundError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for NotFoundError.
2023-10-16 13:56:00 +00:00
func (_ NotFoundError) Error() string {
return "not found"
}
// PreconditionFailedError represents a "Precondition Failed" error.
type PreconditionFailedError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for PreconditionFailedError.
2023-10-16 13:56:00 +00:00
func (_ PreconditionFailedError) Error() string {
return "precondition failed"
}
// RateLimitReachedError represents a "Rate Limit Reached" error.
type RateLimitReachedError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for RateLimitReachedError.
2023-10-16 13:56:00 +00:00
func (_ RateLimitReachedError) Error() string {
return "rate limit reached"
}
// InvalidParametersError represents an "Invalid Parameters" error.
type InvalidParametersError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for InvalidParametersError.
2023-10-16 13:56:00 +00:00
func (_ InvalidParametersError) Error() string {
return "invalid parameters"
2023-10-16 13:56:00 +00:00
}
// InternalServerError represents an "Internal Server Error" error.
type InternalServerError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for InternalServerError.
2023-10-16 13:56:00 +00:00
func (_ InternalServerError) Error() string {
return "internal server error"
}
// BadGatewayError represents a "Bad Gateway" error.
type BadGatewayError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for BadGatewayError.
2023-10-16 13:56:00 +00:00
func (_ BadGatewayError) Error() string {
return "bad gateway"
}
// ServiceUnavailableError represents a "Service Unavailable" error.
type ServiceUnavailableError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for ServiceUnavailableError.
2023-10-16 13:56:00 +00:00
func (_ ServiceUnavailableError) Error() string {
return "service unavailable"
}
// UnknownError represents an "Unknown" error.
type UnknownError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for UnknownError.
2023-10-16 13:56:00 +00:00
func (_ UnknownError) Error() string {
return "unknown error"
}
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
type OriginConnectionTimeOutError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for OriginConnectionTimeOutError.
2023-10-16 13:56:00 +00:00
func (_ OriginConnectionTimeOutError) Error() string {
return "origin connection time-out"
}
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
type SSLHandshakeFailedError struct{}
2023-10-16 13:56:00 +00:00
// Error returns the error message for SSLHandshakeFailedError.
2023-10-16 13:56:00 +00:00
func (_ SSLHandshakeFailedError) Error() string {
return "ssl handshake failed"
}