aboutsummaryrefslogtreecommitdiffstats
path: root/zoned/src/error.rs
blob: 0c28e486e1c8eba8d5ae5dc6066122f807b4417e (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
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
use axum::{
    extract::ws::Message,
    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("Zone Error: {0:?}")]
    Zone(String),

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

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

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

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

    #[error("Missing initialization message")]
    WebSocketMissingInit,

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

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

    #[error("Core Error: {source:?}")]
    Core {
        #[from]
        source: zone_core::Error,
    },

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

    #[error("Axum Error: {source:?}")]
    Axum {
        #[from]
        source: axum::Error,
    },

    #[error("IO Error: {source:?}")]
    IO {
        #[from]
        source: std::io::Error,
    },

    #[error("Json Error: {source:?}")]
    Json {
        #[from]
        source: serde_json::error::Error,
    },

    #[error("Send Error: {source:?}")]
    Send {
        #[from]
        source: tokio::sync::mpsc::error::SendError<Message>,
    },

    #[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::INTERNAL_SERVER_ERROR, format!("{}", err)),
        };

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

        (status, body).into_response()
    }
}