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