aboutsummaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 611ce79d1ffa99a2b5e6fdd7ec287346f64f5034 (plain) (blame)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::string::FromUtf8Error;

use poem::{error::ResponseError, http::StatusCode, IntoResponse};
use thiserror::Error;

use crate::templates;

#[derive(Error, Debug)]
pub enum BlossomError {
    #[error("http client error")]
    Reqwest(reqwest::Error),
    #[error("listenbrainz error")]
    ListenBrainz(listenbrainz::Error),
    #[error("mastadon error")]
    Skinnyverse(mastodon_async::Error),
    #[error("failed to parse timestamp")]
    Chrono(chrono::ParseError),
    #[error("io error")]
    Io(std::io::Error),
    #[error("toml deserialization error")]
    Deserialization(toml::de::Error),
    #[error("atom syndication error")]
    Syndicator(atom_syndication::Error),
    #[error("utf8 conversion error")]
    Utf8Conversion(FromUtf8Error),
    #[error("not found")]
    NotFound,
    #[error("no metadata")]
    NoMetadata,
    #[error("unimplemented")]
    Unimplemented,
    #[error("not a file")]
    NotAFile,
}

impl ResponseError for BlossomError {
    fn status(&self) -> poem::http::StatusCode {
        match self {
            BlossomError::Reqwest(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::ListenBrainz(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Skinnyverse(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Chrono(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Io(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Deserialization(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Syndicator(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Utf8Conversion(_) => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::NotFound => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::NoMetadata => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::Unimplemented => StatusCode::INTERNAL_SERVER_ERROR,
            BlossomError::NotAFile => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn as_response(&self) -> poem::Response
    where
        Self: std::error::Error + Send + Sync + 'static,
    {
        templates::Error {
            status: self.status(),
            message: self.to_string(),
        }
        .into_response()
    }
}

impl From<reqwest::Error> for BlossomError {
    fn from(e: reqwest::Error) -> Self {
        BlossomError::Reqwest(e)
    }
}

impl From<listenbrainz::Error> for BlossomError {
    fn from(e: listenbrainz::Error) -> Self {
        BlossomError::ListenBrainz(e)
    }
}

impl From<mastodon_async::Error> for BlossomError {
    fn from(e: mastodon_async::Error) -> Self {
        BlossomError::Skinnyverse(e)
    }
}

impl From<chrono::ParseError> for BlossomError {
    fn from(e: chrono::ParseError) -> Self {
        BlossomError::Chrono(e)
    }
}

impl From<std::io::Error> for BlossomError {
    fn from(e: std::io::Error) -> Self {
        BlossomError::Io(e)
    }
}

impl From<toml::de::Error> for BlossomError {
    fn from(e: toml::de::Error) -> Self {
        BlossomError::Deserialization(e)
    }
}

impl From<atom_syndication::Error> for BlossomError {
    fn from(e: atom_syndication::Error) -> Self {
        BlossomError::Syndicator(e)
    }
}

impl From<FromUtf8Error> for BlossomError {
    fn from(e: FromUtf8Error) -> Self {
        BlossomError::Utf8Conversion(e)
    }
}