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
use reqwest::{Response, StatusCode};
use crate::config::{HTTP_STATUS_EXPIRED, HTTP_STATUS_UNAUTHORIZED};
use crate::ext::status_code::StatusCodeExt;
pub fn ensure_success(response: &Response) -> Result<(), ResponseError> {
let status = response.status();
if status.is_success() {
return Ok(());
}
if status == HTTP_STATUS_EXPIRED {
return Err(ResponseError::Expired);
}
if status == HTTP_STATUS_UNAUTHORIZED {
return Err(ResponseError::Unauthorized);
}
Err(ResponseError::OtherHttp(status, status.err_text()))
}
#[derive(Fail, Debug)]
pub enum ResponseError {
#[fail(display = "this file has expired or did never exist")]
Expired,
#[fail(display = "unauthorized, are the credentials correct?")]
Unauthorized,
#[fail(display = "bad HTTP response: {}", _1)]
OtherHttp(StatusCode, String),
#[fail(display = "server responded with undefined error")]
Undefined,
}