1use crate::types::ErrorInfo;
2use std::fmt;
3
4#[derive(Debug)]
6pub enum Error {
7 ApiError(u16, Option<ErrorInfo>),
9 JsonError(String),
11 Utf8Error(String),
15 ParseError(String),
17 IoError(String),
19 Reqwest(reqwest::Error),
21
22 MultiCategory(String, String),
25
26 MissingApiToken(String),
28
29 AlreadyInitialized,
31 NotInitialized,
33
34 Other(String),
36}
37
38impl Error {
39 pub fn is_rate_limit(&self) -> bool {
41 if let Error::ApiError(_, Some(info)) = self {
42 if &info.code == "D1" || &info.code == "D2" {
43 return true;
44 }
45 }
46 false
47 }
48}
49
50impl std::error::Error for Error {}
51
52impl From<serde_json::Error> for Error {
53 fn from(e: serde_json::Error) -> Self {
54 Error::JsonError(format!("serde_json: {:?}", e))
55 }
56}
57
58impl From<reqwest::Error> for Error {
59 fn from(e: reqwest::Error) -> Self {
60 Error::Reqwest(e)
61 }
62}
63
64impl From<std::io::Error> for Error {
65 fn from(e: std::io::Error) -> Self {
66 Error::IoError(format!("{:?}", e))
67 }
68}
69
70impl From<std::str::Utf8Error> for Error {
71 fn from(e: std::str::Utf8Error) -> Self {
72 Error::Utf8Error(format!("Invalid UTF-8: {}", e.to_string()))
73 }
74}
75
76impl<T> From<std::sync::PoisonError<T>> for Error {
77 fn from(e: std::sync::PoisonError<T>) -> Self {
78 Error::Other(format!(
79 "Thread died while holding lock. Sorry, you need to quit and start over: {}",
80 e
81 ))
82 }
83}
84
85impl fmt::Display for Error {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 write!(f, "{:#?}", self)
88 }
89}