aboutsummaryrefslogtreecommitdiffstats
path: root/zoned/src/error.rs
blob: 98fbf2f35a5de9a3410c5b999f7de301c6f70073 (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
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,
};
use serde_json::json;
use thiserror::Error;

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

#[derive(Error, Debug)]
pub enum Error {
    #[error("Container Error: {0:?}")]
    Container(String),

    #[error("ZFS Error: {source:?}")]
    ZFS {
        #[from]
        source: zone_zfs::Error,
    },

    #[error("NSpawn Error: {source:?}")]
    Nspawn {
        #[from]
        source: zone_nspawn::Error,
    },

    #[error("Config Error: {source:?}")]
    Config {
        #[from]
        source: figment::Error,
    },

    #[error("Container not found")]
    NotFound,

    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

impl IntoResponse for Error {
    fn into_response(self) -> Response {
        let (status, error_message) = match self {
            Error::Container(source) => (StatusCode::NOT_FOUND, source),
            err => (StatusCode::UNPROCESSABLE_ENTITY, format!("{}", err)),
        };

        let body = Json(json!({
            "error": error_message,
        }));

        (status, body).into_response()
    }
}