grpc_rust/
error.rs

1/*
2 Copyright 2016 LambdaStack All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17use std::error;
18use std::io;
19use std::fmt;
20use std::result;
21use std::str;
22use std::string;
23use rustc_serialize::json;
24
25pub type Result<T> = result::Result<T, Error>;
26
27#[derive(Debug)]
28pub enum Error {
29    CryptoKeyError(String),
30    FileNameError,
31    InvalidTomlError(String),
32    /// Occurs when making lower level IO calls.
33    IO(io::Error),
34    JsonDecode(json::DecoderError),
35    JsonEncode(json::EncoderError),
36    StrFromUtf8Error(str::Utf8Error),
37    StringFromUtf8Error(string::FromUtf8Error),
38    WireDecode(String),
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        let msg = match *self {
44            Error::CryptoKeyError(ref s) => format!("Missing or invalid key: {}", s),
45            Error::FileNameError => format!("Failed to extract a filename"),
46            Error::InvalidTomlError(ref e) => format!("Invalid TOML: {}", e),
47            Error::IO(ref err) => format!("{}", err),
48            Error::JsonDecode(ref e) => format!("JSON decoding error: {}", e),
49            Error::JsonEncode(ref e) => format!("JSON encoding error: {}", e),
50            Error::StrFromUtf8Error(ref e) => format!("{}", e),
51            Error::StringFromUtf8Error(ref e) => format!("{}", e),
52            Error::WireDecode(ref m) => format!("Failed to decode wire message: {}", m),
53        };
54        write!(f, "{}", msg)
55    }
56}
57
58impl error::Error for Error {
59    fn description(&self) -> &str {
60        match *self {
61            Error::CryptoKeyError(_) => "Missing or invalid key",
62            Error::FileNameError => "Failed to extract a filename from a path",
63            Error::InvalidTomlError(_) => "Invalid TOML",
64            Error::IO(ref err) => err.description(),
65            Error::JsonDecode(_) => "JSON decoding error: {:?}",
66            Error::JsonEncode(_) => "JSON encoding error",
67            Error::StrFromUtf8Error(_) => "Failed to convert a string as UTF-8",
68            Error::StringFromUtf8Error(_) => "Failed to convert a string as UTF-8",
69            Error::WireDecode(_) => "Failed to decode wire message",
70        }
71    }
72}
73
74impl From<io::Error> for Error {
75    fn from(err: io::Error) -> Self {
76        Error::IO(err)
77    }
78}
79
80impl From<json::DecoderError> for Error {
81    fn from(err: json::DecoderError) -> Self {
82        Error::JsonDecode(err)
83    }
84}
85
86impl From<json::EncoderError> for Error {
87    fn from(err: json::EncoderError) -> Self {
88        Error::JsonEncode(err)
89    }
90}
91
92impl From<str::Utf8Error> for Error {
93    fn from(err: str::Utf8Error) -> Self {
94        Error::StrFromUtf8Error(err)
95    }
96}
97
98impl From<string::FromUtf8Error> for Error {
99    fn from(err: string::FromUtf8Error) -> Self {
100        Error::StringFromUtf8Error(err)
101    }
102}