Lines
96.15 %
Functions
100 %
Branches
//! The one table from a failure to the HTTP status every web path answers with.
//!
//! Conditions and form failures derive their status from a representative
//! [`CmdError`], so the numbers live only in [`cmd_status`].
use finance::error::FinanceError;
use server::command::CmdError;
use server::db::DBError;
use server::error::ServerError;
use crate::envelope::ErrorCode;
use crate::host_failure::{command_source, condition_code};
const CONFLICT: u16 = 409;
pub(crate) const UNPROCESSABLE: u16 = 422;
pub(crate) const UNAVAILABLE: u16 = 503;
const INTERNAL: u16 = 500;
/// Domain rule violations are the user's to fix; storage failures are ours.
#[must_use]
pub fn finance_status(err: &FinanceError) -> u16 {
match err {
FinanceError::DB(_) | FinanceError::Internal(_) => INTERNAL,
_ => UNPROCESSABLE,
}
pub fn server_status(err: &ServerError) -> u16 {
if err.busy().is_some() {
return CONFLICT;
_ if err.is_unavailable() => UNAVAILABLE,
_ if err.is_missing_row() => 404,
ServerError::Finance(err) => finance_status(err),
ServerError::Creation | ServerError::TagClash { .. } => UNPROCESSABLE,
_ => INTERNAL,
pub fn cmd_status(err: &CmdError) -> u16 {
CmdError::Args(_) => 400,
CmdError::NotFound(_) => 404,
CmdError::NoPrice { .. }
| CmdError::Unkeepable(_)
| CmdError::Refused(_)
| CmdError::ParentGone(_) => UNPROCESSABLE,
CmdError::Disabled => 403,
CmdError::Server(err) => server_status(err),
CmdError::Finance(err) => finance_status(err),
CmdError::Config(_) | CmdError::DB(_) | CmdError::Script(_) => INTERNAL,
/// The command failure a host condition stands for.
fn representative(code: &str) -> CmdError {
match code {
ErrorCode::BUSY => CmdError::Server(DBError::WriteConflict.into()),
ErrorCode::UNAVAILABLE => CmdError::Server(DBError::PoolBudgetExhausted.into()),
ErrorCode::NOT_FOUND => CmdError::NotFound(String::new()),
ErrorCode::ARGS => CmdError::Args(String::new()),
ErrorCode::NO_CONVERSION => CmdError::NoPrice {
from: uuid::Uuid::nil(),
to: uuid::Uuid::nil(),
},
ErrorCode::FORM_INVALID => CmdError::Refused(String::new()),
_ => CmdError::Script(String::new()),
/// The status of a host condition code; an unknown code is ours.
pub fn condition_status(code: &str) -> u16 {
cmd_status(&representative(code))
/// The HTTP status of a native's failure: a condition by its code, anything else ours.
pub fn error_status(err: &wasmtime::Error) -> u16 {
match condition_code(err) {
Some(code) => condition_status(code),
None => command_source(err).map_or(INTERNAL, cmd_status),
#[cfg(test)]
#[path = "status_tests.rs"]
mod tests;