Skip to main content

rpc/forms/
failure.rs

1//! Why a form operation failed, and how each cause is reported.
2
3use std::fmt;
4
5use forms::FieldError;
6use server::command::CmdError;
7use thiserror::Error;
8
9use crate::host_failure::{cmd_failure, rule_violation};
10use crate::status::{cmd_status, error_status};
11
12/// What a form operation addresses by id.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Entity {
15    Transaction,
16    Template,
17    Script,
18    Account,
19    Commodity,
20    Tag,
21}
22
23impl Entity {
24    /// The name opening a sentence, as the commands' not-found messages spell it.
25    fn capitalized(self) -> &'static str {
26        match self {
27            Self::Transaction => "Transaction",
28            Self::Template => "Template",
29            Self::Script => "Script",
30            Self::Account => "Account",
31            Self::Commodity => "Commodity",
32            Self::Tag => "Tag",
33        }
34    }
35}
36
37impl fmt::Display for Entity {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_str(match self {
40            Self::Transaction => "transaction",
41            Self::Template => "template",
42            Self::Script => "script",
43            Self::Account => "account",
44            Self::Commodity => "commodity",
45            Self::Tag => "tag",
46        })
47    }
48}
49
50#[derive(Debug, Error)]
51pub enum Failure {
52    #[error("{} fields invalid", .0.len())]
53    Invalid(Vec<FieldError>),
54    #[error("unknown form '{0}'")]
55    UnknownForm(String),
56    #[error("invalid {entity} id '{id}'")]
57    MalformedId { entity: Entity, id: String },
58    #[error("{} not found", .0.capitalized())]
59    Missing(Entity),
60    #[error("{0}")]
61    Refused(String),
62    #[error("{0}")]
63    Internal(wasmtime::Error),
64}
65
66impl From<wasmtime::Error> for Failure {
67    fn from(err: wasmtime::Error) -> Self {
68        Self::Internal(err)
69    }
70}
71
72impl From<FieldError> for Failure {
73    fn from(error: FieldError) -> Self {
74        Self::Invalid(vec![error])
75    }
76}
77
78impl From<Vec<FieldError>> for Failure {
79    fn from(errors: Vec<FieldError>) -> Self {
80        Self::Invalid(errors)
81    }
82}
83
84impl Failure {
85    /// A rule violation keeps its reason (422); any other cause takes the natives' condition path.
86    pub(crate) fn from_command(context: &str, err: CmdError) -> Self {
87        match rule_violation(&err) {
88            Some(reason) => Self::Refused(reason),
89            None => Self::Internal(cmd_failure(context, err)),
90        }
91    }
92
93    /// The HTTP status a web handler answers with.
94    #[must_use]
95    pub fn http_status(&self) -> u16 {
96        match self {
97            Self::UnknownForm(_) | Self::MalformedId { .. } => {
98                cmd_status(&CmdError::Args(String::new()))
99            }
100            Self::Missing(_) => cmd_status(&CmdError::NotFound(String::new())),
101            Self::Invalid(_) | Self::Refused(_) => cmd_status(&CmdError::Refused(String::new())),
102            Self::Internal(err) => error_status(err),
103        }
104    }
105}