1mod any_http_error;
2pub mod option_ext;
3mod reason;
4pub mod result_ext;
5
6use std::error::Error;
7use std::fmt;
8
9pub use any_http_error::AnyHttpError;
10pub use http::StatusCode;
11use http::{HeaderName, HeaderValue};
12pub use option_ext::OptionExt;
13pub use reason::Reason;
14pub use result_ext::ResultExt;
15
16pub trait HttpError: Error {
17 fn status_code(&self) -> StatusCode;
18
19 fn reason(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 if let Some(reason) = self.status_code().canonical_reason() {
21 f.write_str(reason)?;
22 }
23 Ok(())
24 }
25
26 fn headers(&self) -> Option<Vec<(HeaderName, HeaderValue)>> {
27 None
28 }
29}
30
31impl<E> HttpError for Box<E>
32where
33 E: HttpError,
34{
35 fn status_code(&self) -> StatusCode {
36 HttpError::status_code(&**self)
37 }
38
39 fn reason(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 HttpError::reason(&**self, f)
41 }
42
43 fn headers(&self) -> Option<Vec<(HeaderName, HeaderValue)>> {
44 HttpError::headers(&**self)
45 }
46}
47
48impl<E> From<E> for Box<dyn HttpError>
49where
50 E: HttpError + 'static,
51{
52 fn from(err: E) -> Self {
53 Box::new(err)
54 }
55}