Skip to main content

sdk_rust/
error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display},
4    io,
5};
6
7#[derive(Debug)]
8pub enum SdkError {
9    Io(io::Error),
10    InvalidInput(String),
11    Connection(String),
12    Server(String),
13    Serialization(String),
14}
15
16impl Display for SdkError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::Io(error) => write!(f, "IO error: {error}"),
20            Self::InvalidInput(message) => write!(f, "Invalid input: {message}"),
21            Self::Connection(message) => write!(f, "Connection error: {message}"),
22            Self::Server(message) => write!(f, "Server error: {message}"),
23            Self::Serialization(message) => write!(f, "Serialization error: {message}"),
24        }
25    }
26}
27
28impl Error for SdkError {
29    fn source(&self) -> Option<&(dyn Error + 'static)> {
30        match self {
31            Self::Io(error) => Some(error),
32            Self::InvalidInput(_)
33            | Self::Connection(_)
34            | Self::Server(_)
35            | Self::Serialization(_) => None,
36        }
37    }
38}
39
40impl From<io::Error> for SdkError {
41    fn from(error: io::Error) -> Self {
42        Self::Io(error)
43    }
44}