1use std::fmt;
2
3#[derive(Debug)]
5pub enum Error {
6 Database(sqlx::Error),
7 Config(String),
8 Io(std::io::Error),
9 Json(serde_json::Error),
10 Other(String),
11}
12
13impl fmt::Display for Error {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 match self {
16 Error::Database(e) => write!(f, "Database error: {}", e),
17 Error::Config(e) => write!(f, "Configuration error: {}", e),
18 Error::Io(e) => write!(f, "IO error: {}", e),
19 Error::Json(e) => write!(f, "JSON error: {}", e),
20 Error::Other(e) => write!(f, "Error: {}", e),
21 }
22 }
23}
24
25impl std::error::Error for Error {}
26
27impl From<sqlx::Error> for Error {
28 fn from(e: sqlx::Error) -> Self {
29 Error::Database(e)
30 }
31}
32
33impl From<std::io::Error> for Error {
34 fn from(e: std::io::Error) -> Self {
35 Error::Io(e)
36 }
37}
38
39impl From<serde_json::Error> for Error {
40 fn from(e: serde_json::Error) -> Self {
41 Error::Json(e)
42 }
43}
44
45impl From<anyhow::Error> for Error {
46 fn from(e: anyhow::Error) -> Self {
47 Error::Other(e.to_string())
48 }
49}
50
51impl From<url::ParseError> for Error {
52 fn from(e: url::ParseError) -> Self {
53 Error::Config(format!("Invalid URL: {}", e))
54 }
55}
56
57pub type Result<T> = std::result::Result<T, Error>;