mago_reporting/
error.rs

1use codespan_reporting::files::Error as FilesError;
2use serde_json::Error as JsonError;
3use std::io::Error as IoError;
4
5use mago_database::error::DatabaseError;
6
7#[derive(Debug)]
8pub enum ReportingError {
9    DatabaseError(DatabaseError),
10    JsonError(JsonError),
11    FilesError(FilesError),
12    IoError(IoError),
13    InvalidTarget(String),
14    InvalidFormat(String),
15}
16
17impl ReportingError {
18    pub fn is_broken_pipe(&self) -> bool {
19        let err = match self {
20            Self::IoError(err) => err,
21            Self::FilesError(FilesError::Io(err)) => err,
22            _ => return false,
23        };
24
25        err.raw_os_error() == Some(32) || err.kind() == std::io::ErrorKind::BrokenPipe
26    }
27}
28
29impl std::fmt::Display for ReportingError {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            Self::DatabaseError(error) => write!(f, "{error}"),
33            Self::JsonError(error) => write!(f, "Json error: {error}"),
34            Self::FilesError(error) => write!(f, "Files error: {error}"),
35            Self::IoError(error) => write!(f, "IO error: {error}"),
36            Self::InvalidTarget(target) => write!(f, "Invalid target: {target}"),
37            Self::InvalidFormat(format) => write!(f, "Invalid format: {format}"),
38        }
39    }
40}
41
42impl std::error::Error for ReportingError {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        match self {
45            Self::DatabaseError(error) => Some(error),
46            Self::JsonError(error) => Some(error),
47            Self::FilesError(error) => Some(error),
48            Self::IoError(error) => Some(error),
49            Self::InvalidTarget(_) => None,
50            Self::InvalidFormat(_) => None,
51        }
52    }
53}
54
55impl From<DatabaseError> for ReportingError {
56    fn from(error: DatabaseError) -> Self {
57        Self::DatabaseError(error)
58    }
59}
60
61impl From<JsonError> for ReportingError {
62    fn from(error: JsonError) -> Self {
63        Self::JsonError(error)
64    }
65}
66
67impl From<FilesError> for ReportingError {
68    fn from(error: FilesError) -> Self {
69        Self::FilesError(error)
70    }
71}
72
73impl From<IoError> for ReportingError {
74    fn from(error: IoError) -> Self {
75        Self::IoError(error)
76    }
77}