Skip to main content

rpc/
status.rs

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
6use finance::error::FinanceError;
7use server::command::CmdError;
8use server::db::DBError;
9use server::error::ServerError;
10
11use crate::envelope::ErrorCode;
12use crate::host_failure::{command_source, condition_code};
13
14const CONFLICT: u16 = 409;
15pub(crate) const UNPROCESSABLE: u16 = 422;
16pub(crate) const UNAVAILABLE: u16 = 503;
17const INTERNAL: u16 = 500;
18
19/// Domain rule violations are the user's to fix; storage failures are ours.
20#[must_use]
21pub fn finance_status(err: &FinanceError) -> u16 {
22    match err {
23        FinanceError::DB(_) | FinanceError::Internal(_) => INTERNAL,
24        _ => UNPROCESSABLE,
25    }
26}
27
28#[must_use]
29pub fn server_status(err: &ServerError) -> u16 {
30    if err.busy().is_some() {
31        return CONFLICT;
32    }
33    match err {
34        _ if err.is_unavailable() => UNAVAILABLE,
35        _ if err.is_missing_row() => 404,
36        ServerError::Finance(err) => finance_status(err),
37        ServerError::Creation | ServerError::TagClash { .. } => UNPROCESSABLE,
38        _ => INTERNAL,
39    }
40}
41
42#[must_use]
43pub fn cmd_status(err: &CmdError) -> u16 {
44    if err.busy().is_some() {
45        return CONFLICT;
46    }
47    match err {
48        _ if err.is_unavailable() => UNAVAILABLE,
49        _ if err.is_missing_row() => 404,
50        CmdError::Args(_) => 400,
51        CmdError::NotFound(_) => 404,
52        CmdError::NoPrice { .. }
53        | CmdError::Unkeepable(_)
54        | CmdError::Refused(_)
55        | CmdError::ParentGone(_) => UNPROCESSABLE,
56        CmdError::Disabled => 403,
57        CmdError::Server(err) => server_status(err),
58        CmdError::Finance(err) => finance_status(err),
59        CmdError::Config(_) | CmdError::DB(_) | CmdError::Script(_) => INTERNAL,
60    }
61}
62
63/// The command failure a host condition stands for.
64fn representative(code: &str) -> CmdError {
65    match code {
66        ErrorCode::BUSY => CmdError::Server(DBError::WriteConflict.into()),
67        ErrorCode::UNAVAILABLE => CmdError::Server(DBError::PoolBudgetExhausted.into()),
68        ErrorCode::NOT_FOUND => CmdError::NotFound(String::new()),
69        ErrorCode::ARGS => CmdError::Args(String::new()),
70        ErrorCode::NO_CONVERSION => CmdError::NoPrice {
71            from: uuid::Uuid::nil(),
72            to: uuid::Uuid::nil(),
73        },
74        ErrorCode::FORM_INVALID => CmdError::Refused(String::new()),
75        _ => CmdError::Script(String::new()),
76    }
77}
78
79/// The status of a host condition code; an unknown code is ours.
80#[must_use]
81pub fn condition_status(code: &str) -> u16 {
82    cmd_status(&representative(code))
83}
84
85/// The HTTP status of a native's failure: a condition by its code, anything else ours.
86#[must_use]
87pub fn error_status(err: &wasmtime::Error) -> u16 {
88    match condition_code(err) {
89        Some(code) => condition_status(code),
90        None => command_source(err).map_or(INTERNAL, cmd_status),
91    }
92}
93
94#[cfg(test)]
95#[path = "status_tests.rs"]
96mod tests;