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
use crate::Level;
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("The requested '{0}' with does not exist")]
    NotFound(&'static str, String),

    #[error("Request failed {0}: {1}")]
    RemoteFailed(u16, String),

    #[error(transparent)]
    Serde(#[from] serde_json::Error),

    #[error(transparent)]
    Kv(#[from] worker::kv::KvError),

    #[error(transparent)]
    Worker(#[from] worker::Error),

    #[error("{0}")]
    BadRequest(String),

    #[error("Access Denied")]
    AccessDenied,

    #[error("{0}")]
    // error, XML
    InvalidPoB(String, String),

    #[error("{0}")]
    InvalidId(&'static str),

    #[error("{0}")]
    IOError(#[from] std::io::Error),

    #[error("{0}")]
    Error(String),
}

impl Error {
    pub fn level(&self) -> Level {
        match self {
            Self::NotFound(..) => Level::Info,
            Self::RemoteFailed(..) => Level::Warning,
            Self::Serde(..) => Level::Error,
            Self::Kv(..) => Level::Error,
            Self::Worker(..) => Level::Error,
            Self::BadRequest(..) => Level::Info,
            Self::AccessDenied => Level::Info,
            Self::InvalidId(..) => Level::Info,
            Self::InvalidPoB(..) => Level::Error,
            Self::IOError(..) => Level::Error,
            Self::Error(..) => Level::Error,
        }
    }
}