1use crate::pagination::Object;
5use serde::{Deserialize, Serialize};
6use std::fmt::{Debug, Display, Formatter};
7use std::num::NonZeroU16;
8
9#[derive(Debug, thiserror::Error)]
11pub enum Error {
12 #[error("Invalid UUID: {}", source)]
13 UUID { source: uuid::Error },
14
15 #[error("Invalid Notion API Token: {}", source)]
16 InvalidApiToken {
17 source: reqwest::header::InvalidHeaderValue,
18 },
19
20 #[error("Unable to build reqwest HTTP client: {}", source)]
21 ErrorBuildingClient { source: reqwest::Error },
22
23 #[error("Error sending HTTP request: {}", source)]
24 RequestFailed {
25 #[from]
26 source: reqwest::Error,
27 },
28
29 #[error("Error reading response: {}", source)]
30 ResponseIoError { source: reqwest::Error },
31
32 #[error("Error parsing json response: {}", source)]
33 JsonParseError { source: serde_json::Error },
34
35 #[error("Unexpected API Response")]
36 UnexpectedResponse { response: Object },
37
38 #[error("API Error {}({}): {}", .error.code, .error.status, .error.message)]
39 ApiError { error: ErrorResponse },
40}
41
42#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
43#[serde(transparent)]
44pub struct StatusCode(NonZeroU16);
45
46impl Display for StatusCode {
47 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48 write!(f, "{}", self.0)
49 }
50}
51
52#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
54pub struct ErrorResponse {
55 pub status: StatusCode,
56 pub code: ErrorCode,
57 pub message: String,
58}
59
60#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
62#[serde(rename_all = "snake_case")]
63pub enum ErrorCode {
64 InvalidJson,
66 InvalidRequestUrl,
68 InvalidRequest,
70 ValidationError,
72 MissionVersion,
74 Unauthorized,
76 RestrictedResource,
78 ObjectNotFound,
80 ConflictError,
82 RateLimited,
84 InternalServerError,
86 ServiceUnavailable,
88 DatabaseConnectionUnavailable,
90 #[serde(other)] Unknown,
92}
93
94impl Display for ErrorCode {
95 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
96 write!(f, "{self:?}")
97 }
98}