1use serde::Deserialize;
2use serde_json;
3use std::error::Error as StdError;
4use std::fmt;
5#[derive(Debug)]
6pub struct Error {
7 pub kind: ErrorKind,
8}
9
10impl StdError for Error {}
11
12impl fmt::Display for Error {
13 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14 match &self.kind {
15 ErrorKind::HTTP(_) => {
16 write!(f, "http error")
17 }
18 ErrorKind::Status(err) => {
19 write!(f, "status code: {}, message: {}", err.code, err.message)
20 }
21 ErrorKind::JSON(_) => {
22 write!(f, "json error")
23 }
24 }
25 }
26}
27
28impl From<reqwest::Error> for Error {
29 fn from(e: reqwest::Error) -> Self {
30 Self {
31 kind: ErrorKind::HTTP(e),
32 }
33 }
34}
35
36impl From<serde_json::Error> for Error {
37 fn from(e: serde_json::Error) -> Self {
38 Self {
39 kind: ErrorKind::JSON(e),
40 }
41 }
42}
43
44impl Error {
45 pub fn new(kind: ErrorKind) -> Self {
46 Self { kind }
47 }
48}
49
50#[derive(Debug)]
51pub enum ErrorKind {
52 HTTP(reqwest::Error),
53 Status(StatusError),
54 JSON(serde_json::Error),
55}
56
57#[derive(Debug)]
58pub struct StatusError {
59 pub code: u16,
60 pub message: String,
61}
62
63impl StatusError {
64 pub fn new(code: u16, message: String) -> Self {
65 Self { code, message }
66 }
67}
68#[derive(Deserialize)]
69pub struct ErrorMessage {
70 pub message: String
71}