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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! To generate HTTP error response.
//!
//! ```
//! use sylvia_iot_corelib::err::ErrResp;
//! // To generate HTTP request body format error.
//! if format_error(body) {
//!     return Err(ErrResp::ErrParam(Some("input format error".to_string())));
//! }
//! ```

use std::{error::Error, fmt};

use actix_web::{http::StatusCode, HttpResponse, HttpResponseBuilder, ResponseError};
use serde::Serialize;
use serde_json;

/// The standard error definitions.
#[derive(Debug)]
pub enum ErrResp {
    ErrAuth(Option<String>),
    ErrDb(Option<String>),
    ErrIntMsg(Option<String>),
    ErrNotFound(Option<String>),
    ErrParam(Option<String>),
    ErrPerm(Option<String>),
    ErrRsc(Option<String>),
    ErrUnknown(Option<String>),
    Custom(u16, &'static str, Option<String>),
}

/// Used for generating HTTP body for errors.
#[derive(Serialize)]
struct RespJson<'a> {
    code: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    message: Option<&'a str>,
}

/// 401, token not authorized.
pub const E_AUTH: &'static str = "err_auth";
/// 503, database error.
pub const E_DB: &'static str = "err_db";
/// 503, internal service communication error.
pub const E_INT_MSG: &'static str = "err_int_msg";
/// 404, resource (in path) not found.
pub const E_NOT_FOUND: &'static str = "err_not_found";
/// 400, request (body) format error.
pub const E_PARAM: &'static str = "err_param";
/// 403, invalid permission.
pub const E_PERM: &'static str = "err_perm";
/// 503, allocate resource error.
pub const E_RSC: &'static str = "err_rsc";
/// 500, unknown error.
pub const E_UNKNOWN: &'static str = "err_unknown";

/// To generate error JSON string for HTTP body.
pub fn to_json(code: &str, message: Option<&str>) -> String {
    serde_json::to_string(&RespJson { code, message }).unwrap()
}

impl ErrResp {
    fn resp_json(&self) -> RespJson {
        match *self {
            ErrResp::ErrAuth(ref desc) => RespJson {
                code: E_AUTH,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrDb(ref desc) => RespJson {
                code: E_DB,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrIntMsg(ref desc) => RespJson {
                code: E_INT_MSG,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrNotFound(ref desc) => RespJson {
                code: E_NOT_FOUND,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrParam(ref desc) => RespJson {
                code: E_PARAM,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrPerm(ref desc) => RespJson {
                code: E_PERM,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrRsc(ref desc) => RespJson {
                code: E_RSC,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::ErrUnknown(ref desc) => RespJson {
                code: E_UNKNOWN,
                message: match desc.as_ref() {
                    None => None,
                    Some(desc) => Some(desc.as_str()),
                },
            },
            ErrResp::Custom(_, err_code, ref desc) => RespJson {
                code: err_code,
                message: desc.as_deref(),
            },
        }
    }
}

impl fmt::Display for ErrResp {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", serde_json::to_string(&self.resp_json()).unwrap())
    }
}

impl Error for ErrResp {}

impl ResponseError for ErrResp {
    fn status_code(&self) -> StatusCode {
        match *self {
            ErrResp::ErrAuth(_) => StatusCode::UNAUTHORIZED,
            ErrResp::ErrDb(_) => StatusCode::SERVICE_UNAVAILABLE,
            ErrResp::ErrIntMsg(_) => StatusCode::SERVICE_UNAVAILABLE,
            ErrResp::ErrNotFound(_) => StatusCode::NOT_FOUND,
            ErrResp::ErrParam(_) => StatusCode::BAD_REQUEST,
            ErrResp::ErrPerm(_) => StatusCode::FORBIDDEN,
            ErrResp::ErrRsc(_) => StatusCode::SERVICE_UNAVAILABLE,
            ErrResp::ErrUnknown(_) => StatusCode::INTERNAL_SERVER_ERROR,
            ErrResp::Custom(code, _, _) => StatusCode::from_u16(code).unwrap(),
        }
    }

    fn error_response(&self) -> HttpResponse {
        HttpResponseBuilder::new(self.status_code()).json(self.resp_json())
    }
}