ws_auth/
error.rs

1use serde_derive::{Deserialize, Serialize};
2use std::fmt::{self, Display};
3
4/// Result returning Error
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// All except Internal are considered user-facing.
8#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
9pub enum Error {
10    /// TODO: Document
11    Abort,
12
13    /// TODO: Document
14    Config(String),
15
16    /// TODO: Document
17    Internal(String),
18
19    /// TODO: Document
20    Parse(String),
21
22    /// TODO: Document
23    ReadOnly,
24
25    /// TODO: Document
26    Serialization,
27
28    /// TODO: Document
29    Value(String),
30}
31
32impl std::error::Error for Error {}
33
34impl Display for Error {
35    fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
36        match self {
37            Error::Config(s) | Error::Internal(s) | Error::Parse(s) | Error::Value(s) => {
38                write!(f, "{}", s)
39            }
40            Error::Abort => write!(f, "Operation aborted"),
41            Error::Serialization => write!(f, "Serialization failure, retry transaction"),
42            Error::ReadOnly => write!(f, "Read-only transaction"),
43        }
44    }
45}
46
47impl From<std::array::TryFromSliceError> for Error {
48    fn from(err: std::array::TryFromSliceError) -> Self {
49        Error::Internal(err.to_string())
50    }
51}
52
53impl From<std::io::Error> for Error {
54    fn from(err: std::io::Error) -> Self {
55        Error::Internal(err.to_string())
56    }
57}
58
59impl From<std::net::AddrParseError> for Error {
60    fn from(err: std::net::AddrParseError) -> Self {
61        Error::Internal(err.to_string())
62    }
63}
64
65impl From<std::num::ParseFloatError> for Error {
66    fn from(err: std::num::ParseFloatError) -> Self {
67        Error::Parse(err.to_string())
68    }
69}
70
71impl From<std::num::ParseIntError> for Error {
72    fn from(err: std::num::ParseIntError) -> Self {
73        Error::Parse(err.to_string())
74    }
75}
76
77impl From<std::string::FromUtf8Error> for Error {
78    fn from(err: std::string::FromUtf8Error) -> Self {
79        Error::Internal(err.to_string())
80    }
81}
82
83impl<T> From<std::sync::PoisonError<T>> for Error {
84    fn from(err: std::sync::PoisonError<T>) -> Self {
85        Error::Internal(err.to_string())
86    }
87}