hopr_db_sql/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use hopr_crypto_packet::errors::TicketValidationError;
use hopr_crypto_types::{prelude::CryptoError, types::Hash};
use hopr_db_entity::errors::DbEntityError;
use hopr_internal_types::errors::CoreTypesError;
use hopr_internal_types::tickets::Ticket;
use sea_orm::TransactionError;
use std::sync::Arc;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum DbSqlError {
    #[error("failed to construct the database, {0}")]
    Construction(String),

    #[error("log status not found")]
    MissingLogStatus,

    #[error("log not found")]
    MissingLog,

    #[error("list of logs is empty")]
    EmptyLogsList,

    #[error("log status could not be updated")]
    UpdateLogStatusError,

    #[error("account entry for announcement not found")]
    MissingAccount,

    #[error("channel not found: {0}")]
    ChannelNotFound(Hash),

    #[error("missing fixed entry in table {0}")]
    MissingFixedTableEntry(String),

    #[error("ticket aggregation error: {0}")]
    TicketAggregationError(String),

    #[error("ticket validation error for {}: {}", .0.0, .0.1)]
    TicketValidationError(Box<(Ticket, String)>),

    #[error("transaction error: {0}")]
    TransactionError(Box<dyn std::error::Error + Send + Sync>),

    #[error("error while decoding db entity")]
    DecodingError,

    #[error("logical error: {0}")]
    LogicalError(String),

    #[error("ack validation error: {0}")]
    AcknowledgementValidationError(String),

    #[error(transparent)]
    BackendError(#[from] sea_orm::DbErr),

    #[error(transparent)]
    CoreTypesError(#[from] CoreTypesError),

    #[error(transparent)]
    CryptoError(#[from] CryptoError),

    #[error(transparent)]
    EntityError(#[from] DbEntityError),

    #[error("error while inserting into cache: {0}")]
    CacheError(#[from] Arc<Self>),

    #[error(transparent)]
    NonSpecificError(#[from] hopr_primitive_types::errors::GeneralError),

    #[error(transparent)]
    ApiError(#[from] hopr_db_api::errors::DbError),
}

impl From<TicketValidationError> for DbSqlError {
    fn from(value: TicketValidationError) -> Self {
        DbSqlError::TicketValidationError(Box::new((*value.ticket, value.reason)))
    }
}

impl From<DbSqlError> for hopr_db_api::errors::DbError {
    fn from(value: DbSqlError) -> Self {
        hopr_db_api::errors::DbError::General(value.to_string())
    }
}

impl<E: std::error::Error + Send + Sync + 'static> From<TransactionError<E>> for DbSqlError {
    fn from(value: TransactionError<E>) -> Self {
        match value {
            TransactionError::Connection(e) => Self::BackendError(e),
            TransactionError::Transaction(e) => Self::TransactionError(e.into()),
        }
    }
}

pub type Result<T> = std::result::Result<T, DbSqlError>;