tower_surf/
error.rs

1use http::StatusCode;
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5    /// Maps the [`hmac::digest::InvalidLength`] error.
6    #[error(transparent)]
7    InvalidLength(#[from] hmac::digest::InvalidLength),
8    /// Maps the [`http::header::InvalidHeaderValue`] error.
9    #[error(transparent)]
10    InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
11    /// An expected extension was missing.
12    #[error("couldn't extract `{0}`. is `SurfLayer` enabled?")]
13    ExtensionNotFound(String),
14    /// The token cookie couldn't be found by the name given.
15    #[error("couldn't get cookie")]
16    NoCookie,
17}
18
19impl Error {
20    pub(crate) fn make_layer_error<T: Default, E>(
21        err: impl std::error::Error,
22    ) -> Result<http::Response<T>, E> {
23        tracing::error!(err = %err);
24
25        let mut response = http::Response::default();
26        *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
27
28        Ok(response)
29    }
30
31    pub(crate) fn make_layer_forbidden<T: Default, E>() -> Result<http::Response<T>, E> {
32        let mut response = http::Response::default();
33        *response.status_mut() = StatusCode::FORBIDDEN;
34
35        Ok(response)
36    }
37}