1#![deny(missing_docs)]
2
3#[derive(thiserror::Error, Debug)]
7pub enum Error {
8 #[error("Network error: {0}")]
10 Network(String),
11
12 #[error("HTTP body parse error: {0}")]
14 BodyParse(String),
15
16 #[error("HTTP error {status}: {message}")]
18 Http {
19 status: u16,
21 message: String,
23 },
24
25 #[error("Notion request parameter error: {0}")]
30 RequestParameter(String),
31
32 #[error("Serialization/Deserialization error: {0}")]
34 Serde(#[from] serde_json::Error),
35}
36
37#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
39pub struct ErrorResponse {
40 pub object: String,
42
43 pub status: u16,
45
46 pub code: String,
48
49 pub message: String,
51
52 pub request_id: Option<String>,
54
55 pub developer_survey: Option<String>,
57}
58
59impl Error {
60 pub(crate) async fn try_from_response_async(response: reqwest::Response) -> Self {
61 let status = response.status().as_u16();
62
63 let error_body = match response.text().await{
64 Err(_) =>{
65 return crate::error::Error::Http {
66 status,
67 message: "An error occurred, but failed to retrieve the error details from the response body.".to_string(),
68 }},
69 Ok(body) => body
70 };
71
72 let error_json = serde_json::from_str::<crate::error::ErrorResponse>(&error_body).ok();
73
74 let error_message = match error_json {
75 Some(e) => e.message,
76 None => format!("{:?}", error_body),
77 };
78
79 crate::error::Error::Http {
80 status,
81 message: error_message,
82 }
83 }
84}