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
use reqwest::{Response, StatusCode};
use serde::Deserialize;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum LichessError {
#[error("exceeded request limit")]
RateLimited(Option<usize>),
#[error("request error: {0}")]
Request(#[from] reqwest::Error),
#[error("status code {0}: {1}")]
StatusCode(u16, String),
#[error("lichess error: {0}")]
API(#[from] APIError),
#[error("json parse error: {0}")]
ParseJSON(#[from] serde_json::Error),
#[error("input/output error: {0}")]
IO(#[from] std::io::Error),
}
impl LichessError {
pub(crate) async fn from_response(response: Response) -> Self {
match response.status() {
StatusCode::TOO_MANY_REQUESTS => Self::RateLimited(
response
.headers()
.get(reqwest::header::RETRY_AFTER)
.and_then(|header| header.to_str().ok())
.and_then(|duration| duration.parse().ok()),
),
status => response
.json::<APIError>()
.await
.map(Into::into)
.unwrap_or_else(|_| status.into()),
}
}
}
impl From<StatusCode> for LichessError {
fn from(code: StatusCode) -> Self {
Self::StatusCode(
code.as_u16(),
code.canonical_reason().unwrap_or("unknown").to_string(),
)
}
}
#[derive(Debug, Error, Deserialize)]
#[error("error: {error}")]
pub struct APIError {
error: String,
}