-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.rs
More file actions
119 lines (108 loc) · 3.58 KB
/
error.rs
File metadata and controls
119 lines (108 loc) · 3.58 KB
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
#[derive(Debug)]
pub enum Error {
ApiError {
status: u16,
description: Option<String>,
json: Option<serde_json::Value>,
},
Generic(String),
UnexpectedPayload,
UnhandledStatus(u16),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ApiError { status, description, json: _ } => match description {
Some(string) => write!(f, "{string}"),
None => write!(f, "Unexpected response status {status}"),
},
Self::UnexpectedPayload => write!(f, "Unexpected account info response"),
Self::UnhandledStatus(status) => write!(f, "Unexpected response status {status}"),
Self::Generic(err) => write!(f, "{err}"),
}
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Error::Generic(format!("{value}"))
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Error::Generic(format!("{value}"))
}
}
impl From<url::ParseError> for Error {
fn from(value: url::ParseError) -> Self {
Error::Generic(format!("{value}"))
}
}
impl From<ureq::Error> for Error {
fn from(value: ureq::Error) -> Self {
let desc = format!("{value}");
match value {
ureq::Error::StatusCode(status) => Error::UnhandledStatus(status),
_ => Error::Generic(desc),
}
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use utilities::mocking;
use super::*;
// #[test]
// fn it_can_parse_array_errors() {
// let mut server = mocking::FakeRippling::new();
// let _m = server
// .mock("GET", mocking::Matcher::Any)
// .with_status(400)
// .with_header("content-type", "application/json")
// .with_body(json!(["Oops!"]).to_string())
// .create();
// let req = ureq::get(&server.url()).call();
// match req {
// Ok(ok) => {
// dbg!(ok);
// assert!(false);
// }
// Err(error) => {
// let error: crate::Error = error.into();
// match error {
// Error::ApiError { status, description, json: _ } => {
// assert_eq!(status, 400);
// assert_eq!(description, Some("Oops!".into()));
// }
// _ => assert!(false),
// }
// }
// }
// }
// #[test]
// fn it_can_parse_detail_errors() {
// let mut server = mocking::FakeRippling::new();
// let _m = server
// .mock("GET", mocking::Matcher::Any)
// .with_status(404)
// .with_header("content-type", "application/json")
// .with_body(json!({"detail": "Not found"}).to_string())
// .create();
// let req = ureq::get(&server.url()).call();
// match req {
// Ok(ok) => {
// dbg!(ok);
// assert!(false);
// }
// Err(error) => {
// let error: crate::Error = error.into();
// match error {
// Error::ApiError { status, description, json: _ } => {
// assert_eq!(status, 404);
// assert_eq!(description, Some("Not found".into()));
// }
// _ => assert!(false),
// }
// }
// }
// }
}