Skip to main content

apiplant_core/
error.rs

1//! Error type shared across apiplant crates.
2
3use std::path::PathBuf;
4
5/// Convenient result alias.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything that can go wrong while loading an app or serving a request.
9#[derive(thiserror::Error, Debug)]
10pub enum Error {
11    #[error("i/o error at {path}: {source}")]
12    Io {
13        path: PathBuf,
14        #[source]
15        source: std::io::Error,
16    },
17
18    #[error("invalid TOML in {path}: {source}")]
19    Toml {
20        path: PathBuf,
21        #[source]
22        source: toml::de::Error,
23    },
24
25    #[error("invalid schema for resource `{resource}`: {message}")]
26    Schema { resource: String, message: String },
27
28    #[error("database error: {0}")]
29    Database(String),
30
31    #[error("not found: {0}")]
32    NotFound(String),
33
34    #[error("forbidden: {0}")]
35    Forbidden(String),
36
37    #[error("unauthorized: {0}")]
38    Unauthorized(String),
39
40    #[error("bad request: {0}")]
41    BadRequest(String),
42
43    #[error("plugin error: {0}")]
44    Plugin(String),
45
46    #[error("{0}")]
47    Message(String),
48}
49
50impl Error {
51    /// The HTTP status this error maps to.
52    pub fn status(&self) -> u16 {
53        match self {
54            Error::NotFound(_) => 404,
55            Error::Forbidden(_) => 403,
56            Error::Unauthorized(_) => 401,
57            Error::BadRequest(_) | Error::Schema { .. } => 400,
58            _ => 500,
59        }
60    }
61}