summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: f3399c622500b92dc875cacb5bfb3a7ab374a1d8 (plain)
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
use std::{error, fmt};

use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
};

use crate::id::Id as BaseId;

// I'm making an effort to avoid `anyhow` here, as that crate is _enormously_
// complex (though very usable). We don't need to be overly careful about
// allocations on errors in this app, so this is fine for most "general
// failure" cases.
type BoxedError = Box<dyn error::Error + Send + Sync>;

// Returns a 500 Internal Server Error to the client. Meant to be used via the
// `?` operator; _does not_ return the originating error to the client.
#[derive(Debug)]
pub struct Internal(Id, BoxedError);

impl<E> From<E> for Internal
where
    E: Into<BoxedError>,
{
    fn from(error: E) -> Self {
        let id = Id::generate();
        Self(id, error.into())
    }
}

impl fmt::Display for Internal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self(id, _) = self;
        writeln!(f, "internal server error")?;
        writeln!(f, "error id: {id}")?;
        Ok(())
    }
}

impl IntoResponse for Internal {
    fn into_response(self) -> Response {
        let Self(id, error) = &self;
        eprintln!("hi: [{id}] {error}");
        (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
    }
}

// Transient identifier for an InternalError. Prefixed with `E`.
#[derive(Debug)]
pub struct Id(BaseId);

impl From<BaseId> for Id {
    fn from(id: BaseId) -> Self {
        Self(id)
    }
}

impl Id {
    pub fn generate() -> Self {
        BaseId::generate("E")
    }
}

impl fmt::Display for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

pub struct Unauthorized;

impl IntoResponse for Unauthorized {
    fn into_response(self) -> Response {
        (StatusCode::UNAUTHORIZED, "unauthorized").into_response()
    }
}

pub struct NotFound<E>(pub E);

impl<E> IntoResponse for NotFound<E>
where
    E: std::error::Error,
{
    fn into_response(self) -> Response {
        let Self(response) = self;
        (StatusCode::NOT_FOUND, response.to_string()).into_response()
    }
}