hackmd_api_client_rs/
error.rs

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