wascc_actor/
errors.rs

1// Copyright 2015-2019 Capital One Services, LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Errors
16//!
17//! This module contains types and utility functions for error handling
18
19use std::error::Error as StdError;
20use std::fmt;
21
22#[derive(Debug)]
23pub struct Error(Box<ErrorKind>);
24
25pub(crate) fn new(kind: ErrorKind) -> Error {
26    Error(Box::new(kind))
27}
28
29#[derive(Debug)]
30pub enum ErrorKind {
31    KeyValueError(String),
32    MessagingError(String),
33    MiscError(Box<dyn ::std::error::Error>),
34    EnvVar(std::env::VarError),
35    UTF8(std::string::FromUtf8Error),
36    UTF8Str(std::str::Utf8Error),
37    JsonMarshaling(serde_json::Error),
38    HostError(String),
39    BadDispatch(String),
40    WapcError(wapc::errors::Error),
41}
42
43impl Error {
44    pub fn kind(&self) -> &ErrorKind {
45        &self.0
46    }
47
48    pub fn into_kind(self) -> ErrorKind {
49        *self.0
50    }
51}
52
53impl StdError for Error {
54    fn description(&self) -> &str {
55        match *self.0 {
56            ErrorKind::KeyValueError(_) => "Key/value store error",
57            ErrorKind::UTF8(_) => "UTF8 encoding failure",
58            ErrorKind::MessagingError(_) => "Messaging error",
59            ErrorKind::EnvVar(_) => "Environment variable error",
60            ErrorKind::JsonMarshaling(_) => "JSON encoding/decoding failure",
61            ErrorKind::UTF8Str(_) => "UTF8 encoding failure",
62            ErrorKind::HostError(_) => "Host Error",
63            ErrorKind::BadDispatch(_) => "Bad dispatch",
64            ErrorKind::WapcError(_) => "waPC failure",
65            ErrorKind::MiscError(_) => "Misc error",
66        }
67    }
68
69    fn cause(&self) -> Option<&dyn StdError> {
70        match *self.0 {
71            ErrorKind::KeyValueError(_) => None,
72            ErrorKind::UTF8(ref e) => Some(e),
73            ErrorKind::MessagingError(_) => None,
74            ErrorKind::EnvVar(ref e) => Some(e),
75            ErrorKind::JsonMarshaling(ref e) => Some(e),
76            ErrorKind::UTF8Str(ref e) => Some(e),
77            ErrorKind::HostError(_) => None,
78            ErrorKind::BadDispatch(_) => None,
79            ErrorKind::WapcError(ref e) => Some(e),
80            ErrorKind::MiscError(_) => None,
81        }
82    }
83}
84
85impl fmt::Display for Error {
86    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87        match *self.0 {
88            ErrorKind::KeyValueError(ref msg) => write!(f, "Key/Value error: {}", msg),
89            ErrorKind::UTF8(ref e) => write!(f, "UTF8 encoding error: {}", e),
90            ErrorKind::MessagingError(ref msg) => write!(f, "Messaging error: {}", msg),
91            ErrorKind::EnvVar(ref e) => write!(f, "Environment variable error: {}", e),
92            ErrorKind::JsonMarshaling(ref e) => write!(f, "JSON marshaling error: {}", e),
93            ErrorKind::UTF8Str(ref e) => write!(f, "UTF8 error: {}", e),
94            ErrorKind::HostError(ref e) => write!(f, "Host error: {}", e),
95            ErrorKind::BadDispatch(ref e) => write!(f, "Bad dispatch, attempted operation: {}", e),
96            ErrorKind::WapcError(ref e) => write!(f, "waPC error: {}", e),
97            ErrorKind::MiscError(ref e) => write!(f, "Misc error: {}", e),
98        }
99    }
100}
101
102impl From<wapc::errors::Error> for Error {
103    fn from(source: wapc::errors::Error) -> Error {
104        new(ErrorKind::WapcError(source))
105    }
106}
107
108impl From<std::str::Utf8Error> for Error {
109    fn from(source: std::str::Utf8Error) -> Error {
110        Error(Box::new(ErrorKind::UTF8Str(source)))
111    }
112}
113
114impl From<serde_json::Error> for Error {
115    fn from(source: serde_json::Error) -> Error {
116        Error(Box::new(ErrorKind::JsonMarshaling(source)))
117    }
118}
119
120impl From<std::env::VarError> for Error {
121    fn from(source: std::env::VarError) -> Error {
122        Error(Box::new(ErrorKind::EnvVar(source)))
123    }
124}
125
126impl From<std::string::FromUtf8Error> for Error {
127    fn from(source: std::string::FromUtf8Error) -> Error {
128        Error(Box::new(ErrorKind::UTF8(source)))
129    }
130}
131
132impl From<Box<dyn ::std::error::Error + Send + Sync>> for Error {
133    fn from(source: Box<dyn ::std::error::Error + Send + Sync>) -> Error {
134        Error(Box::new(ErrorKind::MiscError(source)))
135    }
136}
137
138impl From<Box<dyn ::std::error::Error>> for Error {
139    fn from(source: Box<dyn ::std::error::Error>) -> Error {
140        Error(Box::new(ErrorKind::MiscError(source)))
141    }
142}