1
//! The one table from a failure to the HTTP status every web path answers with.
2
//!
3
//! Conditions and form failures derive their status from a representative
4
//! [`CmdError`], so the numbers live only in [`cmd_status`].
5

            
6
use finance::error::FinanceError;
7
use server::command::CmdError;
8
use server::db::DBError;
9
use server::error::ServerError;
10

            
11
use crate::envelope::ErrorCode;
12
use crate::host_failure::{command_source, condition_code};
13

            
14
const CONFLICT: u16 = 409;
15
pub(crate) const UNPROCESSABLE: u16 = 422;
16
pub(crate) const UNAVAILABLE: u16 = 503;
17
const INTERNAL: u16 = 500;
18

            
19
/// Domain rule violations are the user's to fix; storage failures are ours.
20
#[must_use]
21
3875
pub fn finance_status(err: &FinanceError) -> u16 {
22
3875
    match err {
23
1288
        FinanceError::DB(_) | FinanceError::Internal(_) => INTERNAL,
24
2587
        _ => UNPROCESSABLE,
25
    }
26
3875
}
27

            
28
#[must_use]
29
4695
pub fn server_status(err: &ServerError) -> u16 {
30
4695
    if err.busy().is_some() {
31
321
        return CONFLICT;
32
4374
    }
33
4374
    match err {
34
4374
        _ if err.is_unavailable() => UNAVAILABLE,
35
3414
        _ if err.is_missing_row() => 404,
36
973
        ServerError::Finance(err) => finance_status(err),
37
662
        ServerError::Creation | ServerError::TagClash { .. } => UNPROCESSABLE,
38
1458
        _ => INTERNAL,
39
    }
40
4695
}
41

            
42
#[must_use]
43
20440
pub fn cmd_status(err: &CmdError) -> u16 {
44
20440
    if err.busy().is_some() {
45
809
        return CONFLICT;
46
19631
    }
47
19631
    match err {
48
19631
        _ if err.is_unavailable() => UNAVAILABLE,
49
14014
        _ if err.is_missing_row() => 404,
50
977
        CmdError::Args(_) => 400,
51
6285
        CmdError::NotFound(_) => 404,
52
        CmdError::NoPrice { .. }
53
        | CmdError::Unkeepable(_)
54
        | CmdError::Refused(_)
55
1520
        | CmdError::ParentGone(_) => UNPROCESSABLE,
56
339
        CmdError::Disabled => 403,
57
1327
        CmdError::Server(err) => server_status(err),
58
1300
        CmdError::Finance(err) => finance_status(err),
59
1622
        CmdError::Config(_) | CmdError::DB(_) | CmdError::Script(_) => INTERNAL,
60
    }
61
20440
}
62

            
63
/// The command failure a host condition stands for.
64
2754
fn representative(code: &str) -> CmdError {
65
2754
    match code {
66
484
        ErrorCode::BUSY => CmdError::Server(DBError::WriteConflict.into()),
67
1608
        ErrorCode::UNAVAILABLE => CmdError::Server(DBError::PoolBudgetExhausted.into()),
68
327
        ErrorCode::NOT_FOUND => CmdError::NotFound(String::new()),
69
325
        ErrorCode::ARGS => CmdError::Args(String::new()),
70
10
        ErrorCode::NO_CONVERSION => CmdError::NoPrice {
71
10
            from: uuid::Uuid::nil(),
72
10
            to: uuid::Uuid::nil(),
73
10
        },
74
        ErrorCode::FORM_INVALID => CmdError::Refused(String::new()),
75
        _ => CmdError::Script(String::new()),
76
    }
77
2754
}
78

            
79
/// The status of a host condition code; an unknown code is ours.
80
#[must_use]
81
2754
pub fn condition_status(code: &str) -> u16 {
82
2754
    cmd_status(&representative(code))
83
2754
}
84

            
85
/// The HTTP status of a native's failure: a condition by its code, anything else ours.
86
#[must_use]
87
3255
pub fn error_status(err: &wasmtime::Error) -> u16 {
88
3255
    match condition_code(err) {
89
2754
        Some(code) => condition_status(code),
90
501
        None => command_source(err).map_or(INTERNAL, cmd_status),
91
    }
92
3255
}
93

            
94
#[cfg(test)]
95
#[path = "status_tests.rs"]
96
mod tests;