1use thiserror::Error;
2
3use super::entity::JobType;
4
5#[derive(Error, Debug)]
6pub enum JobError {
7 #[error("JobError - Sqlx: {0}")]
8 Sqlx(sqlx::Error),
9 #[error("JobError - InvalidPollInterval: {0}")]
10 InvalidPollInterval(String),
11 #[error("JobError - InvalidJobType: expected '{0}' but initializer was '{1}'")]
12 JobTypeMismatch(JobType, JobType),
13 #[error("JobError - JobInitError: {0}")]
14 JobInitError(String),
15 #[error("JobError - BadData: {0}")]
16 CouldNotSerializeData(serde_json::Error),
17 #[error("JobError - BadState: {0}")]
18 CouldNotSerializeState(serde_json::Error),
19 #[error("JobError - NoInitializerPresent")]
20 NoInitializerPresent,
21 #[error("JobError - JobExecutionError: {0}")]
22 JobExecutionError(String),
23 #[error("JobError - DuplicateId")]
24 DuplicateId,
25}
26
27impl From<Box<dyn std::error::Error>> for JobError {
28 fn from(error: Box<dyn std::error::Error>) -> Self {
29 JobError::JobExecutionError(error.to_string())
30 }
31}
32
33impl From<sqlx::Error> for JobError {
34 fn from(error: sqlx::Error) -> Self {
35 if let Some(err) = error.as_database_error() {
36 if let Some(constraint) = err.constraint() {
37 if constraint.contains("id") {
38 return Self::DuplicateId;
39 }
40 }
41 }
42 Self::Sqlx(error)
43 }
44}