1
use std::path::PathBuf;
2

            
3
use actix_http::error::PayloadError;
4
use actix_web::{http::header::ContentType, HttpResponse};
5

            
6
#[derive(thiserror::Error, Debug)]
7
pub enum Error {
8
    #[error("Error on {path}: {source}")]
9
    Io {
10
        source: std::io::Error,
11
        path: PathBuf,
12
    },
13
    #[error("Error on {path}: {source}")]
14
    Utf8 {
15
        source: std::string::FromUtf8Error,
16
        path: PathBuf,
17
    },
18
    #[error("Authentication error: {source}")]
19
    Authentication {
20
        #[from]
21
        source: jsonwebtoken::errors::Error,
22
    },
23
    #[error("Error on {path:?}: {source}")]
24
    Serde {
25
        source: serde_json::Error,
26
        path: Option<PathBuf>,
27
    },
28
    #[error("{source}")]
29
    Payload {
30
        #[from]
31
        source: PayloadError,
32
    },
33
    #[error("Error: {0}")]
34
    Msg(String),
35
    #[error(transparent)]
36
    Other(#[from] Box<dyn std::error::Error>),
37
}
38

            
39
impl actix_web::error::ResponseError for Error {
40
    fn error_response(&self) -> HttpResponse {
41
        let msg = self.to_string();
42
        HttpResponse::build(self.status_code())
43
            .insert_header(ContentType::html())
44
            .body(msg)
45
    }
46
}
47

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