rusqbin/
errors.rs

1//! Project-specific errors and From implementations to wrap errors from foreign modules.
2
3use serde_json;
4use std::io;
5use hyper;
6use std::sync::PoisonError;
7use regex;
8use url;
9use std::net;
10
11use std::error::Error as StdErr;
12use std::fmt;
13use std::fmt::Display;
14
15/// Project-specfic error enum.
16#[derive(Debug)]
17pub enum Error {
18    PoisonedLock,
19    JsonEncodingError(serde_json::Error),
20    IOError(io::Error),
21    RegexError(regex::Error),
22    UrlParseError(url::ParseError),
23    UnforeseenError,
24    ServerError(hyper::Error),
25    AddressParsingErr(net::AddrParseError),
26    FromUtf8Error,
27    HyperError,
28}
29
30impl<T> From<PoisonError<T>> for Error {
31    fn from(_: PoisonError<T>) -> Self {
32        Error::PoisonedLock
33    }
34}
35
36impl From<serde_json::Error> for Error {
37    fn from(e: serde_json::Error) -> Self {
38        Error::JsonEncodingError(e)
39    }
40}
41
42impl From<io::Error> for Error {
43    fn from(e: io::Error) -> Self {
44        Error::IOError(e)
45    }
46}
47
48impl From<regex::Error> for Error {
49    fn from(e: regex::Error) -> Self {
50        Error::RegexError(e)
51    }
52}
53
54impl From<url::ParseError> for Error {
55    fn from(e: url::ParseError) -> Self {
56        Error::UrlParseError(e)
57    }
58}
59
60impl From<hyper::Error> for Error {
61    fn from(e: hyper::Error) -> Self {
62        Error::ServerError(e)
63    }
64}
65
66impl From<net::AddrParseError> for Error {
67    fn from(e: net::AddrParseError) -> Self {
68        Error::AddressParsingErr(e)
69    }
70}
71
72
73impl Display for Error {
74    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75        // Use `self.number` to refer to each positional data point.
76        use self::Error::*;
77        match self {
78            &PoisonedLock => write!(f, "Poisoned lock error"),
79            &UnforeseenError => write!(f, "Unforeseen error"),
80            &FromUtf8Error => write!(f, "From UTF8 error"),
81            &HyperError => write!(f, "Hyper error"),
82            &AddressParsingErr(ref e) => e.fmt(f),
83            &JsonEncodingError(ref e) => e.fmt(f),
84            &IOError(ref e) => e.fmt(f),
85            &RegexError(ref e) => e.fmt(f),
86            &UrlParseError(ref e) => e.fmt(f),
87            &ServerError(ref e) => e.fmt(f),
88        }
89    }
90}
91
92impl StdErr for Error {
93    fn description(&self) -> &str {
94        use self::Error::*;
95        match self {
96            &PoisonedLock => "Poisoned Lock",
97            &UnforeseenError => "Unforeseen Error",
98            &FromUtf8Error => "UTF8 Conversion Error",
99            &HyperError => "Hyper Error",
100            &AddressParsingErr(ref e) => e.description(),
101            &JsonEncodingError(ref e) => e.description(),
102            &IOError(ref e) => e.description(),
103            &RegexError(ref e) => e.description(),
104            &UrlParseError(ref e) => e.description(),
105            &ServerError(ref e) => e.description(),
106        }
107    }
108
109    fn cause(&self) -> Option<&StdErr> {
110        use self::Error::*;
111        match self {
112            &JsonEncodingError(ref e) => Some(e),
113            &IOError(ref e) => Some(e),
114            &RegexError(ref e) => Some(e),
115            &UrlParseError(ref e) => Some(e),
116            &ServerError(ref e) => Some(e),
117            &AddressParsingErr(ref e) => Some(e),
118            _ => None,
119        }
120    }
121}