spring_web/error.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
#![allow(missing_docs)]
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use spring::error::AppError;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, WebError>;
/// <https://tools.ietf.org/html/rfc7231>
#[derive(Error, Debug)]
#[error("request error, status code is {status_code}: {msg}")]
pub struct KnownWebError {
status_code: StatusCode,
msg: String,
}
macro_rules! impl_known_status_error {
(
$(
$(#[$docs:meta])*
$lower_case:ident, $upper_case:ident,
)+
) => {
impl KnownWebError {
pub fn new<S:Into<String>>(status_code: StatusCode, msg: S) -> Self {
Self {
status_code,
msg: msg.into(),
}
}
$(
$(#[$docs])*
pub fn $lower_case<S:Into<String>>(msg: S) -> Self {
Self::new(StatusCode::$upper_case, msg)
}
)+
}
};
}
impl_known_status_error! (
/// 200 OK
/// [[RFC7231, Section 6.3.1](https://tools.ietf.org/html/rfc7231#section-6.3.1)]
ok, OK,
/// 201 Created
/// [[RFC7231, Section 6.3.2](https://tools.ietf.org/html/rfc7231#section-6.3.2)]
created, CREATED,
/// 202 Accepted
/// [[RFC7231, Section 6.3.3](https://tools.ietf.org/html/rfc7231#section-6.3.3)]
accepted, ACCEPTED,
/// 203 Non-Authoritative Information
/// [[RFC7231, Section 6.3.4](https://tools.ietf.org/html/rfc7231#section-6.3.4)]
non_authoritative_information, NON_AUTHORITATIVE_INFORMATION,
/// 204 No Content
/// [[RFC7231, Section 6.3.5](https://tools.ietf.org/html/rfc7231#section-6.3.5)]
no_content, NO_CONTENT,
/// 205 Reset Content
/// [[RFC7231, Section 6.3.6](https://tools.ietf.org/html/rfc7231#section-6.3.6)]
reset_content, RESET_CONTENT,
/// 206 Partial Content
/// [[RFC7233, Section 4.1](https://tools.ietf.org/html/rfc7233#section-4.1)]
partial_content, PARTIAL_CONTENT,
/// 207 Multi-Status
/// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
multi_status, MULTI_STATUS,
/// 208 Already Reported
/// [[RFC5842](https://tools.ietf.org/html/rfc5842)]
already_reported, ALREADY_REPORTED,
/// 226 IM Used
/// [[RFC3229](https://tools.ietf.org/html/rfc3229)]
im_used, IM_USED,
/// 300 Multiple Choices
/// [[RFC7231, Section 6.4.1](https://tools.ietf.org/html/rfc7231#section-6.4.1)]
multiple_choices, MULTIPLE_CHOICES,
/// 301 Moved Permanently
/// [[RFC7231, Section 6.4.2](https://tools.ietf.org/html/rfc7231#section-6.4.2)]
moved_permanently, MOVED_PERMANENTLY,
/// 302 Found
/// [[RFC7231, Section 6.4.3](https://tools.ietf.org/html/rfc7231#section-6.4.3)]
found, FOUND,
/// 303 See Other
/// [[RFC7231, Section 6.4.4](https://tools.ietf.org/html/rfc7231#section-6.4.4)]
see_other, SEE_OTHER,
/// 304 Not Modified
/// [[RFC7232, Section 4.1](https://tools.ietf.org/html/rfc7232#section-4.1)]
not_modified, NOT_MODIFIED,
/// 305 Use Proxy
/// [[RFC7231, Section 6.4.5](https://tools.ietf.org/html/rfc7231#section-6.4.5)]
use_proxy, USE_PROXY,
/// 307 Temporary Redirect
/// [[RFC7231, Section 6.4.7](https://tools.ietf.org/html/rfc7231#section-6.4.7)]
temporary_redirect, TEMPORARY_REDIRECT,
/// 308 Permanent Redirect
/// [[RFC7238](https://tools.ietf.org/html/rfc7238)]
permanent_redirect, PERMANENT_REDIRECT,
/// 400 Bad Request
/// [[RFC7231, Section 6.5.1](https://tools.ietf.org/html/rfc7231#section-6.5.1)]
bad_request, BAD_REQUEST,
/// 401 Unauthorized
/// [[RFC7235, Section 3.1](https://tools.ietf.org/html/rfc7235#section-3.1)]
unauthorized, UNAUTHORIZED,
/// 402 Payment Required
/// [[RFC7231, Section 6.5.2](https://tools.ietf.org/html/rfc7231#section-6.5.2)]
payment_required, PAYMENT_REQUIRED,
/// 403 Forbidden
/// [[RFC7231, Section 6.5.3](https://tools.ietf.org/html/rfc7231#section-6.5.3)]
forbidden, FORBIDDEN,
/// 404 Not Found
/// [[RFC7231, Section 6.5.4](https://tools.ietf.org/html/rfc7231#section-6.5.4)]
not_found, NOT_FOUND,
/// 405 Method Not Allowed
/// [[RFC7231, Section 6.5.5](https://tools.ietf.org/html/rfc7231#section-6.5.5)]
method_not_allowed, METHOD_NOT_ALLOWED,
/// 406 Not Acceptable
/// [[RFC7231, Section 6.5.6](https://tools.ietf.org/html/rfc7231#section-6.5.6)]
not_acceptable, NOT_ACCEPTABLE,
/// 407 Proxy Authentication Required
/// [[RFC7235, Section 3.2](https://tools.ietf.org/html/rfc7235#section-3.2)]
proxy_authentication_required, PROXY_AUTHENTICATION_REQUIRED,
/// 408 Request Timeout
/// [[RFC7231, Section 6.5.7](https://tools.ietf.org/html/rfc7231#section-6.5.7)]
request_timeout, REQUEST_TIMEOUT,
/// 409 Conflict
/// [[RFC7231, Section 6.5.8](https://tools.ietf.org/html/rfc7231#section-6.5.8)]
conflict, CONFLICT,
/// 410 Gone
/// [[RFC7231, Section 6.5.9](https://tools.ietf.org/html/rfc7231#section-6.5.9)]
gone, GONE,
/// 411 Length Required
/// [[RFC7231, Section 6.5.10](https://tools.ietf.org/html/rfc7231#section-6.5.10)]
length_required, LENGTH_REQUIRED,
/// 412 Precondition Failed
/// [[RFC7232, Section 4.2](https://tools.ietf.org/html/rfc7232#section-4.2)]
precondition_failed, PRECONDITION_FAILED,
/// 413 Payload Too Large
/// [[RFC7231, Section 6.5.11](https://tools.ietf.org/html/rfc7231#section-6.5.11)]
payload_too_large, PAYLOAD_TOO_LARGE,
/// 414 URI Too Long
/// [[RFC7231, Section 6.5.12](https://tools.ietf.org/html/rfc7231#section-6.5.12)]
uri_too_long, URI_TOO_LONG,
/// 415 Unsupported Media Type
/// [[RFC7231, Section 6.5.13](https://tools.ietf.org/html/rfc7231#section-6.5.13)]
unsupported_media_type, UNSUPPORTED_MEDIA_TYPE,
/// 416 Range Not Satisfiable
/// [[RFC7233, Section 4.4](https://tools.ietf.org/html/rfc7233#section-4.4)]
range_not_satisfiable, RANGE_NOT_SATISFIABLE,
/// 417 Expectation Failed
/// [[RFC7231, Section 6.5.14](https://tools.ietf.org/html/rfc7231#section-6.5.14)]
expectation_failed, EXPECTATION_FAILED,
/// 418 I'm a teapot
/// [curiously not registered by IANA but [RFC2324](https://tools.ietf.org/html/rfc2324)]
im_a_teapot, IM_A_TEAPOT,
/// 421 Misdirected Request
/// [RFC7540, Section 9.1.2](https://tools.ietf.org/html/rfc7540#section-9.1.2)
misdirected_request, MISDIRECTED_REQUEST,
/// 422 Unprocessable Entity
/// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
unprocessable_entity, UNPROCESSABLE_ENTITY,
/// 423 Locked
/// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
locked, LOCKED,
/// 424 Failed Dependency
/// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
failed_dependency, FAILED_DEPENDENCY,
/// 426 Upgrade Required
/// [[RFC7231, Section 6.5.15](https://tools.ietf.org/html/rfc7231#section-6.5.15)]
upgrade_required, UPGRADE_REQUIRED,
/// 428 Precondition Required
/// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
precondition_required, PRECONDITION_REQUIRED,
/// 429 Too Many Requests
/// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
too_many_requests, TOO_MANY_REQUESTS,
/// 431 Request Header Fields Too Large
/// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
request_header_fields_too_large, REQUEST_HEADER_FIELDS_TOO_LARGE,
/// 451 Unavailable For Legal Reasons
/// [[RFC7725](https://tools.ietf.org/html/rfc7725)]
unavailable_for_legal_reasons, UNAVAILABLE_FOR_LEGAL_REASONS,
/// 500 Internal Server Error
/// [[RFC7231, Section 6.6.1](https://tools.ietf.org/html/rfc7231#section-6.6.1)]
internal_server_error, INTERNAL_SERVER_ERROR,
/// 501 Not Implemented
/// [[RFC7231, Section 6.6.2](https://tools.ietf.org/html/rfc7231#section-6.6.2)]
not_implemented, NOT_IMPLEMENTED,
/// 502 Bad Gateway
/// [[RFC7231, Section 6.6.3](https://tools.ietf.org/html/rfc7231#section-6.6.3)]
bad_gateway, BAD_GATEWAY,
/// 503 Service Unavailable
/// [[RFC7231, Section 6.6.4](https://tools.ietf.org/html/rfc7231#section-6.6.4)]
service_unavailable, SERVICE_UNAVAILABLE,
/// 504 Gateway Timeout
/// [[RFC7231, Section 6.6.5](https://tools.ietf.org/html/rfc7231#section-6.6.5)]
gateway_timeout, GATEWAY_TIMEOUT,
/// 505 HTTP Version Not Supported
/// [[RFC7231, Section 6.6.6](https://tools.ietf.org/html/rfc7231#section-6.6.6)]
http_version_not_supported, HTTP_VERSION_NOT_SUPPORTED,
/// 506 Variant Also Negotiates
/// [[RFC2295](https://tools.ietf.org/html/rfc2295)]
variant_also_negotiates, VARIANT_ALSO_NEGOTIATES,
/// 507 Insufficient Storage
/// [[RFC4918](https://tools.ietf.org/html/rfc4918)]
insufficient_storage, INSUFFICIENT_STORAGE,
/// 508 Loop Detected
/// [[RFC5842](https://tools.ietf.org/html/rfc5842)]
loop_detected, LOOP_DETECTED,
/// 510 Not Extended
/// [[RFC2774](https://tools.ietf.org/html/rfc2774)]
not_extended, NOT_EXTENDED,
/// 511 Network Authentication Required
/// [[RFC6585](https://tools.ietf.org/html/rfc6585)]
network_authentication_required, NETWORK_AUTHENTICATION_REQUIRED,
);
#[derive(Error, Debug)]
pub enum WebError {
#[error(transparent)]
ResponseStatusError(#[from] KnownWebError),
#[error("Component of type {0} does not exist in the registry")]
ComponentNotExists(&'static str),
#[error("get server config failed for typeof {0}, {1}")]
ConfigDeserializeErr(&'static str, AppError),
#[error(transparent)]
ServerError(#[from] anyhow::Error),
}
// Tell axum how to convert `AppError` into a response.
impl IntoResponse for WebError {
fn into_response(self) -> Response {
match self {
Self::ResponseStatusError(e) => {
tracing::warn!("handler error:{:?}", e);
(e.status_code, e.msg)
}
_other => {
tracing::error!("internal server error:{:?}", _other);
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", _other),
)
}
}
.into_response()
}
}