contained_core/errors/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4    Description: ... Summary ...
5*/
6use super::AsyncError;
7use serde::{Deserialize, Serialize};
8use smart_default::SmartDefault;
9use strum::{Display, EnumString, EnumVariantNames};
10
11#[derive(
12    Clone,
13    Debug,
14    Deserialize,
15    Display,
16    EnumString,
17    EnumVariantNames,
18    Eq,
19    Hash,
20    Ord,
21    PartialEq,
22    PartialOrd,
23    Serialize,
24    SmartDefault,
25)]
26#[strum(serialize_all = "title_case")]
27pub enum Error {
28    AsyncError(AsyncError),
29    CapacityError(String),
30    CompileError(String),
31    ConnectionError(String),
32    ExportError(String),
33    #[default]
34    Error(String),
35    ExecutionError(String),
36    Incomplete(String),
37    RangeError,
38    TypeError,
39    IOError(String),
40    MemoryError(String),
41    NotFound,
42    RecvError(String),
43    SendError(String),
44    StateError,
45    StoreError,
46    TranslateError,
47    TransformError,
48    TapeError,
49    RuntimeError(String),
50    ValidationError,
51}
52
53impl Error {
54    pub fn as_bytes(&self) -> &[u8] {
55        match self {
56            Error::Error(e) => e.as_bytes(),
57            Error::IOError(e) => e.as_bytes(),
58            Error::AsyncError(e) => e.as_bytes(),
59            Error::CompileError(e) => e.as_bytes(),
60            Error::ConnectionError(e) => e.as_bytes(),
61            Error::ExportError(e) => e.as_bytes(),
62            Error::ExecutionError(e) => e.as_bytes(),
63            Error::Incomplete(e) => e.as_bytes(),
64            Error::MemoryError(e) => e.as_bytes(),
65            Error::RecvError(e) => e.as_bytes(),
66            Error::SendError(e) => e.as_bytes(),
67            Error::RuntimeError(e) => e.as_bytes(),
68            Error::TranslateError => b"TranslateError",
69            Error::TransformError => b"TransformError",
70            Error::TapeError => b"TapeError",
71            Error::ValidationError => b"ValidationError",
72            Error::RangeError => b"RangeError",
73            Error::TypeError => b"TypeError",
74            Error::StateError => b"StateError",
75            Error::StoreError => b"StoreError",
76            Error::CapacityError(e) => e.as_bytes(),
77            Error::NotFound => b"NotFound",
78        }
79    }
80}
81
82impl std::error::Error for Error {}
83
84impl From<String> for Error {
85    fn from(error: String) -> Self {
86        Error::Error(error)
87    }
88}
89
90impl From<Box<dyn std::error::Error>> for Error {
91    fn from(error: Box<dyn std::error::Error>) -> Self {
92        Error::Error(error.to_string())
93    }
94}
95
96impl From<Box<dyn std::error::Error + Send + Sync>> for Error {
97    fn from(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
98        Error::AsyncError(error.into())
99    }
100}
101
102impl From<anyhow::Error> for Error {
103    fn from(error: anyhow::Error) -> Self {
104        Error::Error(error.to_string())
105    }
106}
107
108impl From<std::io::Error> for Error {
109    fn from(error: std::io::Error) -> Self {
110        Error::IOError(error.to_string())
111    }
112}
113
114impl From<serde_json::Error> for Error {
115    fn from(error: serde_json::Error) -> Self {
116        Error::Error(error.to_string())
117    }
118}
119
120impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
121    fn from(error: tokio::sync::mpsc::error::SendError<T>) -> Self {
122        Error::AsyncError(error.into())
123    }
124}
125
126impl From<tokio::sync::oneshot::error::RecvError> for Error {
127    fn from(error: tokio::sync::oneshot::error::RecvError) -> Self {
128        Error::AsyncError(error.into())
129    }
130}