1
use finance::error::FinanceError;
2
use scripting::error::HookError;
3
use sqlx::types::uuid::Error as UuidError;
4
use thiserror::Error;
5

            
6
use crate::db::{DBError, is_pool_unavailable};
7

            
8
#[derive(Debug, Error)]
9
pub 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]
42
22691
pub fn root_message(err: &dyn std::error::Error) -> String {
43
22691
    let mut deepest = err;
44
46637
    while let Some(source) = deepest.source() {
45
23946
        deepest = source;
46
    }
47
22691
    deepest.to_string()
48
22691
}
49

            
50
impl 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
57606
    pub fn busy(&self) -> Option<DBError> {
54
57606
        match self {
55
5221
            ServerError::DB(DBError::WriteConflict) => Some(DBError::WriteConflict),
56
            ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err))
57
1
                if crate::tag::lock::is_lock_timeout(err) =>
58
            {
59
364
                Some(DBError::WriteConflict)
60
            }
61
52021
            _ => None,
62
        }
63
57606
    }
64

            
65
    /// True when the database behind the failure cannot be reached right now.
66
52755
    pub fn is_unavailable(&self) -> bool {
67
52755
        match self {
68
24675
            ServerError::DB(err) => err.is_unavailable(),
69
            ServerError::Finance(FinanceError::DB(err)) => is_pool_unavailable(err),
70
28080
            _ => false,
71
        }
72
52755
    }
73

            
74
    /// True when a lookup matched no row.
75
30425
    pub fn is_missing_row(&self) -> bool {
76
30425
        matches!(
77
30425
            self,
78
            ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound))
79
        )
80
30425
    }
81

            
82
    /// A deadlock or serialization failure: the attempt may succeed when run again.
83
3178
    pub(crate) fn is_retryable(&self) -> bool {
84
3178
        match self {
85
            ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err)) => {
86
994
                crate::tag::lock::is_retryable(err)
87
            }
88
2184
            _ => false,
89
        }
90
3178
    }
91
}