Skip to main content

octra_sqlite/client/
error.rs

1use std::fmt;
2
3/// Result alias for octra-sqlite client operations.
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// Error returned by octra-sqlite client operations.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Error {
9    kind: ErrorKind,
10    message: String,
11}
12
13/// Stable category for a client error.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum ErrorKind {
17    Authorization,
18    Config,
19    Decode,
20    Io,
21    Protocol,
22    Receipt,
23    Rpc,
24    Timeout,
25    Transport,
26    Wallet,
27    Other,
28}
29
30impl Error {
31    pub fn new(message: impl Into<String>) -> Self {
32        Self::with_kind(ErrorKind::Other, message)
33    }
34
35    pub fn with_kind(kind: ErrorKind, message: impl Into<String>) -> Self {
36        Self {
37            kind,
38            message: message.into(),
39        }
40    }
41
42    pub fn kind(&self) -> ErrorKind {
43        self.kind
44    }
45}
46
47impl fmt::Display for Error {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.write_str(&self.message)
50    }
51}
52
53impl std::error::Error for Error {}
54
55impl From<crate::protocol::error::Error> for Error {
56    fn from(error: crate::protocol::error::Error) -> Self {
57        Self::with_kind(ErrorKind::Protocol, error.to_string())
58    }
59}
60
61impl From<base64::DecodeError> for Error {
62    fn from(error: base64::DecodeError) -> Self {
63        Self::with_kind(ErrorKind::Decode, error.to_string())
64    }
65}
66
67impl From<hex::FromHexError> for Error {
68    fn from(error: hex::FromHexError) -> Self {
69        Self::with_kind(ErrorKind::Decode, error.to_string())
70    }
71}
72
73impl From<serde_json::Error> for Error {
74    fn from(error: serde_json::Error) -> Self {
75        Self::with_kind(ErrorKind::Decode, error.to_string())
76    }
77}
78
79impl From<std::io::Error> for Error {
80    fn from(error: std::io::Error) -> Self {
81        Self::with_kind(ErrorKind::Io, error.to_string())
82    }
83}
84
85#[cfg(feature = "http")]
86impl From<ureq::Error> for Error {
87    fn from(error: ureq::Error) -> Self {
88        Self::with_kind(ErrorKind::Transport, error.to_string())
89    }
90}