1use crate::response::ApiErrorResponse;
2use std::error;
3use std::fmt;
4
5#[derive(Debug)]
7pub enum ClientError {
8 General(String),
10
11 UnexpectedResponseType(String),
14
15 ApiError(ApiErrorResponse),
17}
18
19impl ClientError {
20 pub fn to_string(&self) -> String {
22 match self {
23 ClientError::General(error) => error.clone(),
24 ClientError::UnexpectedResponseType(error) => error.clone(),
25 ClientError::ApiError(error) => error.to_string(),
26 }
27 }
28}
29
30impl error::Error for ClientError {}
31
32impl fmt::Display for ClientError {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34 match self {
35 ClientError::General(error) => write!(f, "{}", error),
36 ClientError::UnexpectedResponseType(error) => write!(f, "{}", error),
37 ClientError::ApiError(error) => write!(f, "{}", error.to_string()),
38 }
39 }
40}
41
42impl From<String> for ClientError {
43 fn from(err: String) -> ClientError {
44 ClientError::General(err)
45 }
46}
47
48impl From<serde_json::Error> for ClientError {
49 fn from(err: serde_json::Error) -> Self {
50 ClientError::General(err.to_string())
51 }
52}
53
54impl From<serde_urlencoded::ser::Error> for ClientError {
55 fn from(err: serde_urlencoded::ser::Error) -> Self {
56 ClientError::General(err.to_string())
57 }
58}
59
60impl From<&str> for ClientError {
61 fn from(err: &str) -> ClientError {
62 ClientError::General(String::from(err))
63 }
64}
65
66impl From<reqwest::Error> for ClientError {
67 fn from(err: reqwest::Error) -> ClientError {
68 ClientError::General(err.to_string())
69 }
70}