Skip to main content

server/
error.rs

1use finance::error::FinanceError;
2use scripting::error::HookError;
3use sqlx::types::uuid::Error as UuidError;
4use thiserror::Error;
5
6use crate::db::{DBError, is_pool_unavailable};
7
8#[derive(Debug, Error)]
9pub enum ServerError {
10    #[error("Can't create an entity")]
11    Creation,
12    #[error("Password hashing failed")]
13    PasswordHash,
14    #[error("Hook")]
15    Hook(#[from] HookError),
16    #[error("Finance")]
17    Finance(#[from] FinanceError),
18    #[error("Database")]
19    DB(#[from] DBError),
20    #[error("Uuid conversion")]
21    Uuid(#[from] UuidError),
22    #[error("Mutex")]
23    Lock,
24    /// Script-runner orchestration failure (transaction read, script
25    /// execution, or output application). Distinct from `Hook` which
26    /// wraps the in-script executor error type; this variant covers
27    /// the surrounding command-loop bookkeeping.
28    #[error("Script: {0}")]
29    Script(String),
30    #[error("Another tag {name}={value} already exists")]
31    TagClash { name: String, value: String },
32    #[error("{}", crate::tag_state::STALE_TAG)]
33    StaleTag,
34    #[error("{}", crate::template_state::STALE_TEMPLATE)]
35    StaleTemplate,
36    #[error("{}", crate::script_state::STALE_SCRIPT)]
37    StaleScript,
38}
39
40/// The innermost source's text: category variants such as `Finance` only name themselves.
41#[must_use]
42pub fn root_message(err: &dyn std::error::Error) -> String {
43    let mut deepest = err;
44    while let Some(source) = deepest.source() {
45        deepest = source;
46    }
47    deepest.to_string()
48}
49
50impl ServerError {
51    /// The busy condition the failure stands for: a write conflict or a lock
52    /// timeout, both of which the user may simply try again.
53    pub fn busy(&self) -> Option<DBError> {
54        match self {
55            ServerError::DB(DBError::WriteConflict) => Some(DBError::WriteConflict),
56            ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err))
57                if crate::tag::lock::is_lock_timeout(err) =>
58            {
59                Some(DBError::WriteConflict)
60            }
61            _ => None,
62        }
63    }
64
65    /// True when the database behind the failure cannot be reached right now.
66    pub fn is_unavailable(&self) -> bool {
67        match self {
68            ServerError::DB(err) => err.is_unavailable(),
69            ServerError::Finance(FinanceError::DB(err)) => is_pool_unavailable(err),
70            _ => false,
71        }
72    }
73
74    /// True when a lookup matched no row.
75    pub fn is_missing_row(&self) -> bool {
76        matches!(
77            self,
78            ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound))
79        )
80    }
81
82    /// A deadlock or serialization failure: the attempt may succeed when run again.
83    pub(crate) fn is_retryable(&self) -> bool {
84        match self {
85            ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err)) => {
86                crate::tag::lock::is_retryable(err)
87            }
88            _ => false,
89        }
90    }
91}