1use std::fmt::{self, Debug, Display};
2
3use misskey_core::model::ApiError;
4
5pub enum Error<E> {
7 Client(E),
9 API(ApiError),
11 Io(std::io::Error),
13}
14
15impl<E: std::error::Error> std::error::Error for Error<E> {
16 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
17 match self {
18 Error::Client(err) => err.source(),
19 Error::API(err) => Some(err),
20 Error::Io(err) => err.source(),
21 }
22 }
23}
24
25impl<E: std::error::Error> std::fmt::Display for Error<E> {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 match self {
28 Error::Client(err) => Display::fmt(err, f),
29 Error::API(_) => write!(f, "Misskey API returned an error"),
30 Error::Io(err) => Display::fmt(err, f),
31 }
32 }
33}
34
35impl<E: std::error::Error> Debug for Error<E> {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 match self {
38 Error::Client(err) => f.debug_tuple("Client").field(&err).finish(),
39 Error::API(err) => f.debug_tuple("API").field(&err).finish(),
40 Error::Io(err) => f.debug_tuple("Io").field(&err).finish(),
41 }
42 }
43}
44
45impl<E> From<ApiError> for Error<E> {
46 fn from(err: ApiError) -> Self {
47 Error::API(err)
48 }
49}
50
51impl<E> From<std::io::Error> for Error<E> {
52 fn from(err: std::io::Error) -> Self {
53 Error::Io(err)
54 }
55}
56
57#[cfg(feature = "http-client")]
58impl From<misskey_http::Error> for Error<misskey_http::Error> {
59 fn from(err: misskey_http::Error) -> Self {
60 Error::Client(err)
61 }
62}
63
64#[cfg(feature = "websocket-client")]
65impl From<misskey_websocket::Error> for Error<misskey_websocket::Error> {
66 fn from(err: misskey_websocket::Error) -> Self {
67 Error::Client(err)
68 }
69}