backie/src/errors.rs

59 lines
1.8 KiB
Rust
Raw Normal View History

2023-03-04 19:46:09 +00:00
use serde_json::Error as SerdeError;
2023-03-07 16:52:26 +00:00
use std::fmt::Display;
2023-03-04 18:07:17 +00:00
use thiserror::Error;
/// Library errors
2023-03-09 15:59:45 +00:00
#[derive(Debug, Error)]
pub enum BackieError {
QueueProcessingError(#[from] AsyncQueueError),
SerializationError(#[from] SerdeError),
ShutdownError(#[from] tokio::sync::watch::error::SendError<()>),
2023-03-04 18:07:17 +00:00
}
impl Display for BackieError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2023-03-09 15:59:45 +00:00
match self {
BackieError::QueueProcessingError(error) => {
write!(f, "Queue processing error: {}", error)
}
BackieError::SerializationError(error) => write!(f, "Serialization error: {}", error),
BackieError::ShutdownError(error) => write!(f, "Shutdown error: {}", error),
2023-03-04 19:46:09 +00:00
}
}
}
2023-03-04 18:07:17 +00:00
/// List of error types that can occur while working with cron schedules.
#[derive(Debug, Error)]
pub enum CronError {
/// A problem occured during cron schedule parsing.
#[error(transparent)]
LibraryError(#[from] cron::error::Error),
/// [`Scheduled`] enum variant is not provided
#[error("You have to implement method `cron()` in your AsyncRunnable")]
TaskNotSchedulableError,
/// The next execution can not be determined using the current [`Scheduled::CronPattern`]
#[error("No timestamps match with this cron pattern")]
NoTimestampsError,
}
#[derive(Debug, Error)]
pub enum AsyncQueueError {
#[error(transparent)]
PgError(#[from] diesel::result::Error),
2023-03-04 18:07:17 +00:00
#[error(transparent)]
SerdeError(#[from] serde_json::Error),
2023-03-04 18:07:17 +00:00
#[error(transparent)]
CronError(#[from] CronError),
#[error("Task is not in progress, operation not allowed")]
TaskNotRunning,
2023-03-04 18:07:17 +00:00
}
impl From<cron::error::Error> for AsyncQueueError {
fn from(error: cron::error::Error) -> Self {
AsyncQueueError::CronError(CronError::LibraryError(error))
}
}