cngateway/
e.rs

1// Implement bitcoin and bdk error type.
2use std::fmt::Display;
3use std::fmt::Formatter;
4use serde_derive::{Deserialize, Serialize};
5
6
7#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
8pub enum ErrorKind {
9  Key,
10  Wallet,
11  Network,
12  Input,
13  NoResource,
14  Internal,
15}
16
17impl Display for ErrorKind {
18  fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
19    match self {
20      ErrorKind::Input => write!(f, "Input"),
21      ErrorKind::Internal => write!(f, "OpError"),
22      ErrorKind::Key => write!(f, "KeyError"),
23      ErrorKind::Wallet => write!(f, "WalletError"),
24      ErrorKind::Network => write!(f, "NetworkError"),
25      ErrorKind::NoResource => write!(f, "NoResourceFound"),
26
27    }
28  }
29}
30
31impl PartialEq for ErrorKind {
32  fn eq(&self,rs: &ErrorKind) -> bool {
33    self.to_string() == rs.to_string()
34  }
35}
36
37
38#[derive(Serialize, Deserialize, Debug, Clone)]
39pub struct S5Error {
40  pub kind: String,
41  pub message: String,
42}
43
44impl S5Error {
45  pub fn new(kind: ErrorKind, message: &str) -> Self {
46    S5Error {
47      kind: kind.to_string(),
48      message: message.to_string(),
49    }
50  }
51  // pub fn from_ureq(e: ureq::Error)->Self{
52  //   match e {
53  //     ureq::Error::Status(code, response) => {
54  //       let kind = match code {
55  //           400 => ErrorKind::Input,
56  //           401 => ErrorKind::Key,
57  //           403 => ErrorKind::Key,
58  //           404 => ErrorKind::NoResource,
59  //           409 => ErrorKind::Input,
60  //           _=> ErrorKind::Internal
61  //       };
62  //       S5Error::new(kind, &response.into_string().unwrap())
63  //     }
64  //     _ => { 
65  //       S5Error::new(ErrorKind::Network, "Transport Error. Check your internet connection AND/OR your request object.")
66  //     }
67  //   }
68  // }
69
70}