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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use rocket::http::ContentType;
pub use rocket::http::Status;
use rocket::response::Responder;
use rocket::Request;
use serde_json;
use std::fmt::Display;
use std::io::Cursor;

pub trait ApiResultExt<T, E> {
    fn private_context<D>(self, context: D) -> Result<T, WebError>
    where
        D: Display + Send + Sync + 'static,
        Self: Sized;

    fn public_context<D>(self, context: D) -> Result<T, WebError>
    where
        D: Display + Send + Sync + 'static,
        Self: Sized;

    fn publish_error(self) -> Result<T, WebError>
    where
        Self: Sized;

    fn bad_request(self) -> Result<T, WebError>
    where
        Self: Sized;

    fn forbidden(self) -> Result<T, WebError>
    where
        Self: Sized;

    fn not_found(self) -> Result<T, WebError>
    where
        Self: Sized;
}

impl<T, E> ApiResultExt<T, E> for Result<T, E>
where
    E: WebFail,
{
    fn private_context<D>(self, context: D) -> Result<T, WebError>
    where
        D: Display + Send + Sync + 'static,
        Self: Sized,
    {
        self.map_err(|err| err.fail().with_private_error(context))
    }

    fn public_context<D>(self, context: D) -> Result<T, WebError>
    where
        D: Display + Send + Sync + 'static,
        Self: Sized,
    {
        self.map_err(|err| err.fail().with_public_error(context))
    }

    fn publish_error(self) -> Result<T, WebError>
    where
        Self: Sized,
    {
        self.map_err(|err| err.fail().publish_error())
    }

    fn bad_request(self) -> Result<T, WebError>
    where
        Self: Sized,
    {
        // 400
        self.map_err(|err| err.fail().with_status(Status::BadRequest))
    }

    fn forbidden(self) -> Result<T, WebError>
    where
        Self: Sized,
    {
        // 403
        self.map_err(|err| err.fail().with_status(Status::Forbidden))
    }

    fn not_found(self) -> Result<T, WebError>
    where
        Self: Sized,
    {
        // 404
        self.map_err(|err| err.fail().with_status(Status::NotFound))
    }
}

pub type ApiResult<T> = ::std::result::Result<T, WebError>;

#[derive(Debug)]
pub enum ApiResponse<T> {
    Success(T),
    Error(WebError),
}

impl<T> ApiResponse<T> {
    pub fn into_strict(self) -> (StrictApiResponse<T>, ResponseHints) {
        match self {
            ApiResponse::Success(x) => (StrictApiResponse::Success(x), ResponseHints::ok()),
            ApiResponse::Error(x) => {
                let msg = x.public_error();
                let hints = ResponseHints::err(&x);
                (StrictApiResponse::Error(msg), hints)
            },
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub enum StrictApiResponse<T> {
    #[serde(rename = "success")]
    Success(T),
    #[serde(rename = "error")]
    Error(String),
}

pub struct ResponseHints {
    status: Status,
}

impl ResponseHints {
    pub fn ok() -> ResponseHints {
        ResponseHints {
            status: Status::Ok,
        }
    }

    pub fn err(err: &WebError) -> ResponseHints {
        let status = err.status.unwrap_or(Status::InternalServerError);

        ResponseHints {
            status,
        }
    }
}

impl<'r, T> Responder<'r> for ApiResponse<T>
where
    T: serde::Serialize,
{
    fn respond_to(self, _req: &Request) -> ::std::result::Result<rocket::Response<'r>, Status> {
        let (response, hints) = self.into_strict();
        let body = serde_json::to_string(&response).map_err(|_| Status::InternalServerError)?;

        rocket::Response::build()
            .status(hints.status)
            .header(ContentType::JSON)
            .sized_body(Cursor::new(body))
            .ok()
    }
}

#[derive(Debug)]
pub struct WebError {
    status: Option<Status>,
    private_error: String,
    public_error: Option<String>,
}

impl WebError {
    pub fn new<D: Display>(error: D) -> WebError {
        WebError {
            status: None,
            private_error: error.to_string(),
            public_error: None,
        }
    }

    pub fn with_status(mut self, status: Status) -> WebError {
        self.status = Some(status);
        self
    }

    pub fn with_private_error<D: Display>(mut self, error: D) -> WebError {
        self.private_error = error.to_string();
        self
    }

    pub fn with_public_error<D: Display>(mut self, error: D) -> WebError {
        self.public_error = Some(error.to_string());
        self
    }

    pub fn publish_error(mut self) -> WebError {
        self.public_error = Some(self.private_error.clone());
        self
    }

    pub fn public_error(&self) -> String {
        self.public_error.as_ref()
            .map(|x| x.to_string())
            .unwrap_or_else(|| match self.status {
                Some(status) => status.reason,
                _ => Status::InternalServerError.reason,
            }.to_string())
    }
}

impl<E: Display + Send + Sync + 'static> From<E> for WebError {
    fn from(e: E) -> WebError {
        WebError::new(e)
    }
}

impl<'r> Responder<'r> for WebError {
    fn respond_to(self, _req: &Request) -> ::std::result::Result<rocket::Response<'r>, Status> {
        ApiResponse::Error::<()>(self).respond_to(_req)
    }
}

pub trait WebFail: Send + Sync + 'static {
    fn fail(self) -> WebError
    where
        Self: Sized;
}

impl<E: Display + Send + Sync + 'static> WebFail for E {
    fn fail(self) -> WebError
    where
        Self: Sized,
    {
        WebError::new(self)
    }
}

impl WebFail for WebError {
    fn fail(self) -> WebError
    where
        Self: Sized,
    {
        self
    }
}

pub fn err_msg<D: Display + Sync + Send + 'static>(status: Status, msg: D) -> WebError {
    WebError {
        status: Some(status),
        private_error: msg.to_string(),
        public_error: Some(msg.to_string()),
    }
}