summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 2d512e60e965cda9b883fcd3ac4106cf3525c099 (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
use std::error;

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

// 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.
//
// If that changes, my hope is to use `thiserror` or something with a similar
// strategy, before resorting to `anyhow`.
pub 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 InternalError;

impl<E> From<E> for InternalError
where
    E: Into<BoxedError>,
{
    fn from(_: E) -> InternalError {
        // At some point it may be useful for this to record the originating
        // error so that it can be logged… -oj
        InternalError
    }
}

impl IntoResponse for InternalError {
    fn into_response(self) -> Response {
        (StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
    }
}