1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::StdError;

/// Representation of a Server Error raised by a http server
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ServerError {
    /// error message
    pub message: String,
}

impl ServerError {
    /// InternalServerError factory
    pub fn new<M: Into<String>>(message: M) -> ServerError {
        ServerError {
            message: message.into(),
        }
    }
}

impl Display for ServerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl From<String> for ServerError {
    fn from(message: String) -> Self {
        ServerError::new(message)
    }
}

impl From<&str> for ServerError {
    fn from(message: &str) -> Self {
        ServerError::new(message.to_string())
    }
}

impl From<StdError> for ServerError {
    fn from(error: StdError) -> Self {
        ServerError::new(format!("{error:?}"))
    }
}

/// Representation of a Client Error raised by an http server
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ClientError {
    /// error label
    pub label: String,

    /// error message
    pub message: String,
}

impl ClientError {
    /// ClientError factory
    pub fn new<L: Into<String>, M: Into<String>>(label: L, message: M) -> ClientError {
        ClientError {
            label: label.into(),
            message: message.into(),
        }
    }
}

impl Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.label, self.message)
    }
}