1use crate::{DynHttpError, HttpError, IntoHttpErrorResponse};
4use http::StatusCode;
5use std::{error::Error, fmt::Display};
6
7#[derive(Debug)]
13pub struct AnyhowHttpError {
14 error: anyhow::Error,
16 status: StatusCode,
18}
19
20impl Error for AnyhowHttpError {
21 fn source(&self) -> Option<&(dyn Error + 'static)> {
22 self.error.source()
23 }
24}
25
26impl Display for AnyhowHttpError {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 Display::fmt(&self.error, f)
29 }
30}
31
32impl HttpError for AnyhowHttpError {
33 #[cfg(feature = "log")]
34 fn log(&self) {
35 log::error!("{:?}", self.error);
37 }
38
39 fn status(&self) -> StatusCode {
40 self.status
41 }
42
43 #[cfg(feature = "hide-anyhow")]
44 fn reason(&self) -> String {
45 "Server error".to_string()
47 }
48}
49
50impl<I> From<anyhow::Error> for DynHttpError<I>
53where
54 I: IntoHttpErrorResponse,
55{
56 fn from(value: anyhow::Error) -> Self {
57 value
58 .status(StatusCode::INTERNAL_SERVER_ERROR)
60 .into()
62 }
63}
64
65pub trait AnyhowStatusExt {
67 fn status(self, status: StatusCode) -> AnyhowHttpError;
69}
70
71impl AnyhowStatusExt for anyhow::Error {
72 fn status(self, status: StatusCode) -> AnyhowHttpError {
73 AnyhowHttpError {
74 error: self,
75 status,
76 }
77 }
78}