1use http::StatusCode;
4
5use super::Error;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum HttpError {
22 BadRequest,
24 Unauthorized,
26 Forbidden,
28 NotFound,
30 MethodNotAllowed,
32 Conflict,
34 Gone,
36 UnprocessableEntity,
38 TooManyRequests,
40 PayloadTooLarge,
42 InternalServerError,
44 BadGateway,
46 ServiceUnavailable,
48 GatewayTimeout,
50}
51
52impl HttpError {
53 pub fn status_code(self) -> StatusCode {
55 match self {
56 Self::BadRequest => StatusCode::BAD_REQUEST,
57 Self::Unauthorized => StatusCode::UNAUTHORIZED,
58 Self::Forbidden => StatusCode::FORBIDDEN,
59 Self::NotFound => StatusCode::NOT_FOUND,
60 Self::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
61 Self::Conflict => StatusCode::CONFLICT,
62 Self::Gone => StatusCode::GONE,
63 Self::UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY,
64 Self::TooManyRequests => StatusCode::TOO_MANY_REQUESTS,
65 Self::PayloadTooLarge => StatusCode::PAYLOAD_TOO_LARGE,
66 Self::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
67 Self::BadGateway => StatusCode::BAD_GATEWAY,
68 Self::ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE,
69 Self::GatewayTimeout => StatusCode::GATEWAY_TIMEOUT,
70 }
71 }
72
73 pub fn message(self) -> &'static str {
75 match self {
76 Self::BadRequest => "Bad Request",
77 Self::Unauthorized => "Unauthorized",
78 Self::Forbidden => "Forbidden",
79 Self::NotFound => "Not Found",
80 Self::MethodNotAllowed => "Method Not Allowed",
81 Self::Conflict => "Conflict",
82 Self::Gone => "Gone",
83 Self::UnprocessableEntity => "Unprocessable Entity",
84 Self::TooManyRequests => "Too Many Requests",
85 Self::PayloadTooLarge => "Payload Too Large",
86 Self::InternalServerError => "Internal Server Error",
87 Self::BadGateway => "Bad Gateway",
88 Self::ServiceUnavailable => "Service Unavailable",
89 Self::GatewayTimeout => "Gateway Timeout",
90 }
91 }
92}
93
94impl From<HttpError> for Error {
96 fn from(http_err: HttpError) -> Self {
97 Error::new(http_err.status_code(), http_err.message())
98 }
99}