etsi014_client/
error.rs

1use std::fmt;
2
3pub(crate) type BoxError = Box<dyn std::error::Error>;
4
5#[derive(Debug)]
6pub enum ErrorType {
7    InvalidHost,
8    InvalidArgument,
9    ConnectionError,
10    InvalidResponse,
11}
12#[derive(Debug)]
13pub struct Error {
14    pub msg: String,
15    pub kind: ErrorType,
16    pub source: Option<BoxError>,
17}
18
19impl Error {
20    pub(crate) fn new(
21        msg: String,
22        kind: ErrorType,
23        source: Option<Box<dyn std::error::Error>>,
24    ) -> Error {
25        Error { msg, kind, source }
26    }
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        let caused_by = match self.source {
32            Some(ref e) => format!("\n\nCaused by: {:#?}", e),
33            None => "".to_string(),
34        };
35        write!(f, "{}{}", self.msg, caused_by)
36    }
37}
38
39impl std::error::Error for Error {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        self.source.as_deref()
42    }
43}