e621-sdk-go/pkg/e621/utils/error.go

107 lines
2.5 KiB
Go
Raw Normal View History

package utils
import "fmt"
// StatusCodesToError maps HTTP status codes to corresponding error types.
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{}
func (_ AccessDeniedError) Error() string {
return "access denied"
}
// NotFoundError represents a "Not Found" error.
type NotFoundError struct{}
func (_ NotFoundError) Error() string {
return "not found"
}
// PreconditionFailedError represents a "Precondition Failed" error.
type PreconditionFailedError struct{}
func (_ PreconditionFailedError) Error() string {
return "precondition failed"
}
// RateLimitReachedError represents a "Rate Limit Reached" error.
type RateLimitReachedError struct{}
func (_ RateLimitReachedError) Error() string {
return "rate limit reached"
}
// InvalidParametersError represents an "Invalid Parameters" error.
type InvalidParametersError struct{}
func (_ InvalidParametersError) Error() string {
return "invalid parameters"
}
// InternalServerError represents an "Internal Server Error" error.
type InternalServerError struct{}
func (_ InternalServerError) Error() string {
return "internal server error"
}
// BadGatewayError represents a "Bad Gateway" error.
type BadGatewayError struct{}
func (_ BadGatewayError) Error() string {
return "bad gateway"
}
// ServiceUnavailableError represents a "Service Unavailable" error.
type ServiceUnavailableError struct{}
func (_ ServiceUnavailableError) Error() string {
return "service unavailable"
}
// UnknownError represents an "Unknown" error.
type UnknownError struct{}
func (_ UnknownError) Error() string {
return "unknown error"
}
// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error.
type OriginConnectionTimeOutError struct{}
func (_ OriginConnectionTimeOutError) Error() string {
return "origin connection time-out"
}
// SSLHandshakeFailedError represents an "SSL Handshake Failed" error.
type SSLHandshakeFailedError struct{}
func (_ SSLHandshakeFailedError) Error() string {
return "ssl handshake failed"
}