Lines
96.43 %
Functions
100 %
Branches
use finance::error::FinanceError;
use scripting::error::HookError;
use sqlx::types::uuid::Error as UuidError;
use thiserror::Error;
use crate::db::{DBError, is_pool_unavailable};
#[derive(Debug, Error)]
pub enum ServerError {
#[error("Can't create an entity")]
Creation,
#[error("Password hashing failed")]
PasswordHash,
#[error("Hook")]
Hook(#[from] HookError),
#[error("Finance")]
Finance(#[from] FinanceError),
#[error("Database")]
DB(#[from] DBError),
#[error("Uuid conversion")]
Uuid(#[from] UuidError),
#[error("Mutex")]
Lock,
/// Script-runner orchestration failure (transaction read, script
/// execution, or output application). Distinct from `Hook` which
/// wraps the in-script executor error type; this variant covers
/// the surrounding command-loop bookkeeping.
#[error("Script: {0}")]
Script(String),
#[error("Another tag {name}={value} already exists")]
TagClash { name: String, value: String },
#[error("{}", crate::tag_state::STALE_TAG)]
StaleTag,
#[error("{}", crate::template_state::STALE_TEMPLATE)]
StaleTemplate,
#[error("{}", crate::script_state::STALE_SCRIPT)]
StaleScript,
}
/// The innermost source's text: category variants such as `Finance` only name themselves.
#[must_use]
pub fn root_message(err: &dyn std::error::Error) -> String {
let mut deepest = err;
while let Some(source) = deepest.source() {
deepest = source;
deepest.to_string()
impl ServerError {
/// The busy condition the failure stands for: a write conflict or a lock
/// timeout, both of which the user may simply try again.
pub fn busy(&self) -> Option<DBError> {
match self {
ServerError::DB(DBError::WriteConflict) => Some(DBError::WriteConflict),
ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err))
if crate::tag::lock::is_lock_timeout(err) =>
{
Some(DBError::WriteConflict)
_ => None,
/// True when the database behind the failure cannot be reached right now.
pub fn is_unavailable(&self) -> bool {
ServerError::DB(err) => err.is_unavailable(),
ServerError::Finance(FinanceError::DB(err)) => is_pool_unavailable(err),
_ => false,
/// True when a lookup matched no row.
pub fn is_missing_row(&self) -> bool {
matches!(
self,
ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound))
)
/// A deadlock or serialization failure: the attempt may succeed when run again.
pub(crate) fn is_retryable(&self) -> bool {
ServerError::DB(DBError::Sqlx(err)) | ServerError::Finance(FinanceError::DB(err)) => {
crate::tag::lock::is_retryable(err)