diff options
author | 2021-08-29 12:59:04 -0700 | |
---|---|---|
committer | 2021-08-29 12:59:04 -0700 | |
commit | 1c22088640e5deeadd61017bd90920b652685b5f (patch) | |
tree | 59d110a7c8792c39e0c1ea180fa972eee1c4662d /src/models/error.rs | |
parent | 8d5779a661f4537ef7d87a44bdfbf240eb054ff1 (diff) | |
download | notion-1c22088640e5deeadd61017bd90920b652685b5f.tar.gz notion-1c22088640e5deeadd61017bd90920b652685b5f.tar.zst notion-1c22088640e5deeadd61017bd90920b652685b5f.zip |
Notion API Version 2021-08-16 support; fix a bunch of modling errors as well
Diffstat (limited to 'src/models/error.rs')
-rw-r--r-- | src/models/error.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/models/error.rs b/src/models/error.rs new file mode 100644 index 0000000..e382719 --- /dev/null +++ b/src/models/error.rs @@ -0,0 +1,70 @@ +use serde::{Deserialize, Serialize}; +use std::fmt::{Display, Formatter}; + +#[derive(Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] +#[serde(transparent)] +pub struct StatusCode(u16); + +impl StatusCode { + pub fn code(&self) -> u16 { + self.0 + } +} + +impl Display for StatusCode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +/// <https://developers.notion.com/reference/errors> +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)] +pub struct ErrorResponse { + pub status: StatusCode, + pub code: ErrorCode, + pub message: String, +} + +/// <https://developers.notion.com/reference/errors> +#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ErrorCode { + InvalidJson, + InvalidRequestUrl, + InvalidRequest, + ValidationError, + MissionVersion, + Unauthorized, + RestrictedResource, + ObjectNotFound, + ConflictError, + RateLimited, + InternalServerError, + ServiceUnavailable, + #[serde(other)] // serde issue #912 + Unknown, +} + +impl Display for ErrorCode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + +#[cfg(test)] +mod tests { + use crate::models::error::{ErrorCode, ErrorResponse}; + + #[test] + fn deserialize_error() { + let error: ErrorResponse = serde_json::from_str(include_str!("tests/error.json")).unwrap(); + assert_eq!(error.code, ErrorCode::ValidationError) + } + + #[test] + fn deserialize_unknown_error() { + let error: ErrorResponse = + serde_json::from_str(include_str!("tests/unknown_error.json")).unwrap(); + assert_eq!(error.code, ErrorCode::Unknown) + } +} |