wascc_host/
errors.rs

1//! Custom error types
2use std::error::Error as StdError;
3use std::fmt;
4
5#[derive(Debug)]
6pub struct Error(Box<ErrorKind>);
7
8pub fn new(kind: ErrorKind) -> Error {
9    Error(Box::new(kind))
10}
11
12#[derive(Debug)]
13pub enum ErrorKind {
14    Wapc(wapc::errors::Error),
15    HostCallFailure(Box<dyn StdError + Send + Sync>),
16    Wascap(wascap::Error),
17    Authorization(String),
18    IO(std::io::Error),
19    CapabilityProvider(String),
20    MiscHost(String),
21    Plugin(libloading::Error),
22    Middleware(String),
23    Serialization(String),
24}
25
26impl Error {
27    pub fn kind(&self) -> &ErrorKind {
28        &self.0
29    }
30
31    pub fn into_kind(self) -> ErrorKind {
32        *self.0
33    }
34}
35
36impl StdError for Error {
37    fn description(&self) -> &str {
38        match *self.0 {
39            ErrorKind::Wapc(_) => "waPC error",
40            ErrorKind::IO(_) => "I/O error",
41            ErrorKind::HostCallFailure(_) => "Error occurred during host call",
42            ErrorKind::Wascap(_) => "Embedded JWT Failure",
43            ErrorKind::Authorization(_) => "Module authorization failure",
44            ErrorKind::CapabilityProvider(_) => "Capability provider failure",
45            ErrorKind::MiscHost(_) => "waSCC Host error",
46            ErrorKind::Plugin(_) => "Plugin error",
47            ErrorKind::Middleware(_) => "Middleware error",
48            ErrorKind::Serialization(_) => "Serialization failure",
49        }
50    }
51
52    fn cause(&self) -> Option<&dyn StdError> {
53        match *self.0 {
54            ErrorKind::Wapc(ref err) => Some(err),
55            ErrorKind::HostCallFailure(_) => None,
56            ErrorKind::Wascap(ref err) => Some(err),
57            ErrorKind::Authorization(_) => None,
58            ErrorKind::IO(ref err) => Some(err),
59            ErrorKind::CapabilityProvider(_) => None,
60            ErrorKind::MiscHost(_) => None,
61            ErrorKind::Plugin(ref err) => Some(err),
62            ErrorKind::Middleware(_) => None,
63            ErrorKind::Serialization(_) => None,
64        }
65    }
66}
67
68impl fmt::Display for Error {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        match *self.0 {
71            ErrorKind::Wapc(ref err) => write!(f, "waPC failure: {}", err),
72            ErrorKind::HostCallFailure(ref err) => {
73                write!(f, "Error occurred during host call: {}", err)
74            }
75            ErrorKind::Wascap(ref err) => write!(f, "Embedded JWT failure: {}", err),
76            ErrorKind::Authorization(ref err) => {
77                write!(f, "WebAssembly module authorization failure: {}", err)
78            }
79            ErrorKind::IO(ref err) => write!(f, "I/O error: {}", err),
80            ErrorKind::CapabilityProvider(ref err) => {
81                write!(f, "Capability provider error: {}", err)
82            }
83            ErrorKind::MiscHost(ref err) => write!(f, "waSCC Host Error: {}", err),
84            ErrorKind::Plugin(ref err) => write!(f, "Plugin error: {}", err),
85            ErrorKind::Middleware(ref err) => write!(f, "Middleware error: {}", err),
86            ErrorKind::Serialization(ref err) => write!(f, "Serialization failure: {}", err),
87        }
88    }
89}
90
91impl From<libloading::Error> for Error {
92    fn from(source: libloading::Error) -> Error {
93        Error(Box::new(ErrorKind::Plugin(source)))
94    }
95}
96impl From<wascap::Error> for Error {
97    fn from(source: wascap::Error) -> Error {
98        Error(Box::new(ErrorKind::Wascap(source)))
99    }
100}
101
102impl From<wapc::errors::Error> for Error {
103    fn from(source: wapc::errors::Error) -> Error {
104        Error(Box::new(ErrorKind::Wapc(source)))
105    }
106}
107
108impl From<std::io::Error> for Error {
109    fn from(source: std::io::Error) -> Error {
110        Error(Box::new(ErrorKind::IO(source)))
111    }
112}
113
114impl From<Box<dyn StdError + Send + Sync>> for Error {
115    fn from(source: Box<dyn StdError + Send + Sync>) -> Error {
116        Error(Box::new(ErrorKind::HostCallFailure(source)))
117    }
118}
119
120impl From<String> for Error {
121    fn from(source: String) -> Error {
122        Error(Box::new(ErrorKind::MiscHost(source)))
123    }
124}
125
126#[cfg(test)]
127mod tests {
128    #[allow(dead_code)]
129    fn assert_sync_send<T: Send + Sync>() {}
130    const _: fn() = || assert_sync_send::<super::Error>();
131}