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
//! Errors that can occur when working with cookies.
use crate::error::UnexpectedError;
use crate::response::Response;
pub use biscotti::errors::*;
use http::header::ToStrError;

#[derive(Debug)]
#[non_exhaustive]
/// The error type returned by [`extract_request_cookies`](super::extract_request_cookies).
pub enum ExtractRequestCookiesError {
    InvalidHeaderValue(ToStrError),
    MissingPair(MissingPairError),
    EmptyName(EmptyNameError),
    Crypto(CryptoError),
    Decoding(DecodingError),
    Unexpected(UnexpectedError),
}

impl std::fmt::Display for ExtractRequestCookiesError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ExtractRequestCookiesError::InvalidHeaderValue(_) => {
                write!(
                    f,
                    "Some characters in the `Cookie` header aren't printable ASCII characters"
                )
            }
            _ => {
                write!(
                    f,
                    "Failed to parse request cookies out of the `Cookie` header"
                )
            }
        }
    }
}

impl std::error::Error for ExtractRequestCookiesError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            ExtractRequestCookiesError::InvalidHeaderValue(e) => Some(e),
            ExtractRequestCookiesError::MissingPair(e) => Some(e),
            ExtractRequestCookiesError::EmptyName(e) => Some(e),
            ExtractRequestCookiesError::Crypto(e) => Some(e),
            ExtractRequestCookiesError::Decoding(e) => Some(e),
            ExtractRequestCookiesError::Unexpected(e) => Some(e),
        }
    }
}

impl ExtractRequestCookiesError {
    /// Convert an [`ExtractRequestCookiesError`] into an HTTP response.
    ///
    /// It returns a `400 Bad Request` to the caller.
    /// The body provides details on what exactly went wrong.
    pub fn into_response(&self) -> Response {
        use std::fmt::Write as _;

        let mut body = self.to_string();
        match self {
            ExtractRequestCookiesError::MissingPair(e) => {
                write!(body, ". {e}").ok();
            }
            ExtractRequestCookiesError::EmptyName(e) => {
                write!(body, ". {e}").ok();
            }
            ExtractRequestCookiesError::Decoding(e) => {
                write!(body, ". {e}").ok();
            }
            ExtractRequestCookiesError::Unexpected(_)
            | ExtractRequestCookiesError::InvalidHeaderValue(_)
            | ExtractRequestCookiesError::Crypto(_) => {}
        }
        Response::bad_request().set_typed_body(body)
    }
}

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
#[error("Some characters in the `Set-Cookie` header value are not printable ASCII characters.")]
/// The error type returned by [`inject_response_cookies`](super::inject_response_cookies).
pub struct InjectResponseCookiesError {
    /// The invalid header value.
    pub invalid_header_value: String,
}

impl InjectResponseCookiesError {
    /// Convert an [`InjectResponseCookiesError`] into an HTTP response.
    ///
    /// It returns a `500 Internal Server Error` to the caller,
    /// since failure is likely due to misconfiguration or
    /// mismanagement on the server side.
    pub fn into_response(&self) -> Response {
        Response::internal_server_error()
    }
}