glitch_in_the_matrix/
errors.rs

1//! Error handling.
2macro_rules! derive_from {
3    ($err:ident, $($var:ident, $ty:ty),*) => {
4        $(
5            impl From<$ty> for $err {
6                fn from(e: $ty) -> $err {
7                    $err::$var(e)
8                }
9            }
10         )*
11    }
12}
13use failure::Fail;
14
15/// Something Matrixy that can go wrong.
16#[derive(Fail, Debug)]
17#[allow(missing_docs)]
18pub enum MatrixError {
19    #[fail(display = "HTTP error: {}", _0)]
20    Hyper(#[cause] ::hyper::error::Error),
21    #[fail(display = "Serialization error: {}", _0)]
22    Serde(#[cause] ::serde_json::Error),
23    #[fail(display = "Error decoding URI: {}", _0)]
24    UriError(#[cause] ::hyper::http::uri::InvalidUri),
25    #[fail(display = "I/O error: {}", _0)]
26    Io(#[cause] ::std::io::Error),
27    #[fail(display = "OpenSSL error: {}", _0)]
28    Openssl(#[cause] ::hyper_openssl::openssl::error::ErrorStack),
29    /// A request failed with a non-OK HTTP status.
30    ///
31    /// If the body contained a valid `BadRequestReply`, the `BadRequest` variant will be used
32    /// instead of this one.
33    #[fail(display = "Request failed with HTTP status: {}", _0)]
34    HttpCode(::http::status::StatusCode),
35    #[fail(display = "Error in HTTP library: {}", _0)]
36    HttpError(::http::Error),
37    #[fail(display = "Invalid header value: {}", _0)]
38    InvalidHeaderValue(::http::header::InvalidHeaderValue),
39    /// A request failed with an error from the homeserver.
40    #[fail(display = "Error from homeserver: {:?}", _0)]
41    BadRequest(super::types::replies::BadRequestReply)
42}
43derive_from!(MatrixError,
44             Hyper, ::hyper::error::Error,
45             Serde, ::serde_json::Error,
46             UriError, ::hyper::http::uri::InvalidUri,
47             HttpError, ::http::Error,
48             InvalidHeaderValue, ::http::header::InvalidHeaderValue,
49             Io, ::std::io::Error,
50             Openssl, ::hyper_openssl::openssl::error::ErrorStack
51            );
52/// Bog-standard result newtype. You know the drill.
53pub type MatrixResult<T> = Result<T, MatrixError>;
54