/
error.rs
88 lines (74 loc) · 2.14 KB
/
error.rs
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
use feignhttp::{get, ErrorKind};
#[get("httpbin.org/anything")]
async fn url_error() -> feignhttp::Result<()> {}
#[get(url = "https://httpbin.org/delay/3", timeout = "abc")]
async fn config_error() -> feignhttp::Result<()> {}
#[get(url = "https://httpbin.org/delay/3", timeout = 2000)]
async fn timeout_error() -> feignhttp::Result<()> {}
#[get(url = "https://httpbin.org/123")]
async fn status_error() -> feignhttp::Result<()> {}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct User {
id: i32,
}
#[cfg(feature = "json")]
#[get(url = "https://httpbin.org/anything")]
async fn decode_error() -> feignhttp::Result<User> {}
#[tokio::main]
async fn main() {
match url_error().await {
Err(err) => {
// Build client error.
if err.is_build_error() {
println!("url_error: {}", err);
}
}
_ => {}
}
match config_error().await {
Err(err) => {
// Parse config error.
if err.is_config_error() {
println!("config_error: {}", err);
}
}
_ => {}
}
match timeout_error().await {
Err(err) => {
// Request error.
if err.is_request_error() {
println!("timeout_error: {}", err);
}
}
_ => {}
}
#[cfg(feature = "json")]
match decode_error().await {
Err(err) => {
// Decode error.
if err.is_decode_error() {
println!("decode_error: {}", err);
}
}
_ => {}
}
match status_error().await {
Err(err) => {
// Status error.
if err.is_status_error() {
println!("status_error: {}", err);
}
if let ErrorKind::Status(status) = err.error_kind() {
println!("status error code: {}", status.as_u16());
if status.is_client_error() {
// Handle error.
}
if status.is_server_error() {
// Handle error.
}
}
}
_ => {}
}
}