effectum/
error.rs

1use deadpool_sqlite::InteractError;
2
3/// A [std::result::Result] whose error type defaults to [Error].
4pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6/// Errors that can be returned from the queue.
7#[derive(thiserror::Error, Debug)]
8pub enum Error {
9    /// An error occurred while updating the database to a new schema version.
10    #[error("Migration error: {0}")]
11    Migration(#[from] rusqlite_migration::Error),
12    /// An error occurred while opening the database.
13    #[error("Error opening database: {0}")]
14    OpenDatabase(eyre::Report),
15    /// Failed to acquire a database connection for reading.
16    #[error("Error acquiring database connection: {0}")]
17    PoolError(#[from] deadpool_sqlite::PoolError),
18    /// Encountered an error communicating with the database.
19    #[error("Database error: {0}")]
20    Database(#[from] rusqlite::Error),
21    /// The database contained invalid data.
22    #[error("Unexpected value type for {1}: {0}")]
23    ColumnType(#[source] rusqlite::Error, &'static str),
24    /// An internal task panicked.
25    #[error("Internal error: {0}")]
26    Panic(#[from] tokio::task::JoinError),
27    /// An internal error occurred while reading the database.
28    #[error("Internal error: {0}")]
29    DbInteract(String),
30    /// When requesting a job status, the job ID was not found.
31    #[error("Job not found")]
32    NotFound,
33    /// A job had an unknown state value
34    #[error("Invalid job state {0}")]
35    InvalidJobState(String),
36    /// Failed to serialize or deserialize information when recording information about a job run.
37    #[error("Error decoding job run info {0}")]
38    InvalidJobRunInfo(serde_json::Error),
39    /// Failed to serialize or deserialize a job payload
40    #[error("Error processing payload: {0}")]
41    PayloadError(serde_json::Error),
42    /// Invalid value for a job timestamp
43    #[error("Timestamp {0} out of range")]
44    TimestampOutOfRange(&'static str),
45    /// A worker attempted to finish a job more than once.
46    #[error("Attempted to finish already-finished job")]
47    JobAlreadyConsumed,
48    /// The operation timed out. This is mostly used when the queue fails to shut down in a timely
49    /// fashion.
50    #[error("Timed out")]
51    Timeout,
52    /// The job could not be modified or cancelled because it is currently running.
53    #[error("Job is running")]
54    JobRunning,
55    /// The job could not be modified or cancelled because it has already finished.
56    #[error("Job is finished")]
57    JobFinished,
58    /// The current job has expired.
59    #[error("Job expired")]
60    Expired,
61    /// An unregistered worker tried to communicate with the queue.
62    #[error("Worker {0} not found")]
63    WorkerNotFound(u64),
64    /// Indicates that the queue has closed, and so the attempted operation could not be completed.
65    #[error("Queue closed unexpectedly")]
66    QueueClosed,
67    /// The passed schedule specification is invalid
68    #[error("Invalid schedule specification")]
69    InvalidSchedule,
70    /// A recurring job with the same ID already exists
71    #[error("Recurring job {0} already exists")]
72    RecurringJobAlreadyExists(String),
73}
74
75impl Error {
76    /// Returns if the error indicates that an update or cancel request occurred too late
77    pub fn is_update_too_late(&self) -> bool {
78        matches!(self, Error::JobRunning | Error::JobFinished)
79    }
80}
81
82impl From<InteractError> for Error {
83    fn from(e: InteractError) -> Self {
84        Error::DbInteract(e.to_string())
85    }
86}
87
88impl Error {
89    pub(crate) fn open_database(err: impl Into<eyre::Report>) -> Self {
90        Error::OpenDatabase(err.into())
91    }
92}