Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
use failure, not error-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeeeta committed Mar 3, 2018
1 parent 178d205 commit d0d6dff
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
**/*.rs.bk
Cargo.lock
*.swp
3 changes: 2 additions & 1 deletion glitch-in-the-matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ repository = "https://github.com/eeeeeta/glitch-in-the-matrix"
version = "0.13.1"

[dependencies]
error-chain = "0.10"
failure = "0.1"
failure_derive = "0.1"
futures = "0.1"
hyper = "0.11"
hyper-openssl = "0.3"
Expand Down
57 changes: 35 additions & 22 deletions glitch-in-the-matrix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,50 @@ extern crate serde;
#[macro_use] extern crate serde_json;
extern crate hyper;
extern crate hyper_openssl;
#[macro_use] extern crate error_chain;
extern crate failure;
#[macro_use] extern crate failure_derive;
extern crate tokio_core;
#[macro_use] extern crate futures;
extern crate percent_encoding;
pub extern crate gm_types as types;

pub mod errors {
#![allow(unused_doc_comment)]
//! Error handling, using `error_chain`.
//!
//! The `StatusCode` enum is re-exported in the `http` module.
error_chain! {
types {
MatrixError, MatrixErrorKind, ResultExt, MatrixResult;
}
foreign_links {
Hyper(::hyper::error::Error);
Serde(::serde_json::Error);
UriError(::hyper::error::UriError);
Io(::std::io::Error);
Openssl(::hyper_openssl::openssl::error::ErrorStack);
}
errors {
HttpCode(c: ::hyper::StatusCode) {
display("HTTP error: {}", c.canonical_reason().unwrap_or("unknown"))
}
BadRequest(e: super::types::replies::BadRequestReply) {
display("Bad request: {:?}", e)
macro_rules! derive_from {
($err:ident, $($var:ident, $ty:ty),*) => {
$(
impl From<$ty> for $err {
fn from(e: $ty) -> $err {
$err::$var(e)
}
}
)*
}
}
#[derive(Fail, Debug)]
pub enum MatrixError {
#[fail(display = "HTTP error: {}", _0)]
Hyper(#[cause] ::hyper::error::Error),
#[fail(display = "Serialization error: {}", _0)]
Serde(#[cause] ::serde_json::Error),
#[fail(display = "Error decoding URI: {}", _0)]
UriError(#[cause] ::hyper::error::UriError),
#[fail(display = "I/O error: {}", _0)]
Io(#[cause] ::std::io::Error),
#[fail(display = "OpenSSL error: {}", _0)]
Openssl(#[cause] ::hyper_openssl::openssl::error::ErrorStack),
#[fail(display = "Request failed with HTTP status: {}", _0)]
HttpCode(::hyper::StatusCode),
#[fail(display = "Error from homeserver: {:?}", _0)]
BadRequest(super::types::replies::BadRequestReply)
}
derive_from!(MatrixError,
Hyper, ::hyper::error::Error,
Serde, ::serde_json::Error,
UriError, ::hyper::error::UriError,
Io, ::std::io::Error,
Openssl, ::hyper_openssl::openssl::error::ErrorStack
);
pub type MatrixResult<T> = Result<T, MatrixError>;
}
pub mod http {
//! Types reexported from `hyper`.
Expand Down
2 changes: 1 addition & 1 deletion glitch-in-the-matrix/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'a, 'b, 'c> RoomClient<'a, 'b, 'c> {
x.users_default
}
}).or_else(|e| {
if let &MatrixErrorKind::BadRequest(ref brk) = e.kind() {
if let MatrixError::BadRequest(ref brk) = e {
if brk.errcode == "M_NOT_FOUND" {
return Ok(0)
}
Expand Down
5 changes: 2 additions & 3 deletions glitch-in-the-matrix/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Utility wrappers used internally.
use errors::*;
use errors::MatrixErrorKind::*;
use types::replies::*;
use hyper::{Body, StatusCode};
use hyper::client::Response;
Expand Down Expand Up @@ -29,10 +28,10 @@ impl<T: DeserializeOwned> ResponseWrapper<T> {
let resp = try_ready!(self.inner.poll());
if !self.sc.is_success() {
if let Ok(e) = ::serde_json::from_slice::<BadRequestReply>(&resp) {
bail!(BadRequest(e));
return Err(MatrixError::BadRequest(e));
}
else {
bail!(HttpCode(self.sc.clone()));
return Err(MatrixError::HttpCode(self.sc.clone()));
}
}
Ok(Async::Ready(resp))
Expand Down

0 comments on commit d0d6dff

Please sign in to comment.