hackmd_api_client_rs/
error.rs1use reqwest::header;
2use std::{error, fmt, result};
3
4#[derive(Debug)]
5pub struct HackMDError {
6 pub message: String,
7}
8
9impl fmt::Display for HackMDError {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 write!(f, "{}", self.message)
12 }
13}
14
15impl error::Error for HackMDError {}
16
17#[derive(Debug)]
18pub struct HttpResponseError {
19 pub message: String,
20 pub code: u16,
21 pub status_text: String,
22}
23
24impl fmt::Display for HttpResponseError {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "{} ({})", self.message, self.code)
27 }
28}
29
30impl error::Error for HttpResponseError {}
31
32#[derive(Debug)]
33pub struct MissingRequiredArgument {
34 pub message: String,
35}
36
37impl fmt::Display for MissingRequiredArgument {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 write!(f, "{}", self.message)
40 }
41}
42
43impl error::Error for MissingRequiredArgument {}
44
45#[derive(Debug)]
46pub struct InternalServerError {
47 pub message: String,
48 pub code: u16,
49 pub status_text: String,
50}
51
52impl fmt::Display for InternalServerError {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 write!(f, "{} ({})", self.message, self.code)
55 }
56}
57
58impl error::Error for InternalServerError {}
59
60#[derive(Debug)]
61pub struct TooManyRequestsError {
62 pub message: String,
63 pub code: u16,
64 pub status_text: String,
65 pub user_limit: u32,
66 pub user_remaining: u32,
67 pub reset_after: Option<u64>,
68}
69
70impl fmt::Display for TooManyRequestsError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(
73 f,
74 "{} ({}): {}/{} requests remaining",
75 self.message, self.code, self.user_remaining, self.user_limit
76 )
77 }
78}
79
80impl error::Error for TooManyRequestsError {}
81
82#[derive(Debug)]
83pub enum ApiError {
84 HackMD(HackMDError),
85 HttpResponse(HttpResponseError),
86 MissingRequiredArgument(MissingRequiredArgument),
87 InternalServer(InternalServerError),
88 TooManyRequests(TooManyRequestsError),
89 Reqwest(reqwest::Error),
90 Url(url::ParseError),
91 Header(header::InvalidHeaderValue),
92 Serde(serde_json::Error),
93}
94
95impl fmt::Display for ApiError {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 match self {
98 ApiError::HackMD(err) => write!(f, "HackMD error: {}", err),
99 ApiError::HttpResponse(err) => write!(f, "HTTP response error: {}", err),
100 ApiError::MissingRequiredArgument(err) => {
101 write!(f, "Missing required argument: {}", err)
102 }
103 ApiError::InternalServer(err) => write!(f, "Internal server error: {}", err),
104 ApiError::TooManyRequests(err) => write!(f, "Too many requests: {}", err),
105 ApiError::Reqwest(err) => write!(f, "Request error: {}", err),
106 ApiError::Url(err) => write!(f, "URL parse error: {}", err),
107 ApiError::Header(err) => write!(f, "Header error: {}", err),
108 ApiError::Serde(err) => write!(f, "Serialization error: {}", err),
109 }
110 }
111}
112
113impl error::Error for ApiError {}
114
115impl From<reqwest::Error> for ApiError {
116 fn from(error: reqwest::Error) -> Self {
117 ApiError::Reqwest(error)
118 }
119}
120
121impl From<url::ParseError> for ApiError {
122 fn from(error: url::ParseError) -> Self {
123 ApiError::Url(error)
124 }
125}
126
127impl From<header::InvalidHeaderValue> for ApiError {
128 fn from(error: header::InvalidHeaderValue) -> Self {
129 ApiError::Header(error)
130 }
131}
132
133impl From<serde_json::Error> for ApiError {
134 fn from(error: serde_json::Error) -> Self {
135 ApiError::Serde(error)
136 }
137}
138
139pub type Result<T> = result::Result<T, ApiError>;