summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerris Tseng <ferristseng@gmail.com>2018-08-04 12:04:04 -0400
committerGitHub <noreply@github.com>2018-08-04 12:04:04 -0400
commitc37a49f137637c1041186143788460e5ddf28822 (patch)
treefc391010f988df107916331629211573afde8ba2
parent8ddbbd1854f86a15d522247993e6c6ce39db1781 (diff)
parent7692800f95117cedeca2c40725186b0feacb1187 (diff)
Merge pull request #17 from leodasvacas/switch-from-error-chain-to-failure
Switch from error-chain to failure
-rw-r--r--ipfs-api/Cargo.toml2
-rw-r--r--ipfs-api/src/client.rs8
-rw-r--r--ipfs-api/src/lib.rs2
-rw-r--r--ipfs-api/src/read.rs6
-rw-r--r--ipfs-api/src/response/error.rs102
5 files changed, 77 insertions, 43 deletions
diff --git a/ipfs-api/Cargo.toml b/ipfs-api/Cargo.toml
index 597a42a..9fe3bd7 100644
--- a/ipfs-api/Cargo.toml
+++ b/ipfs-api/Cargo.toml
@@ -15,7 +15,7 @@ travis-ci = { repository = "ferristseng/rust-ipfs-api" }
[dependencies]
bytes = "0.4"
-error-chain = "0.12"
+failure = "0.1.2"
futures = "0.1"
http = "0.1"
hyper = "0.12"
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs
index 2e07b29..c933e89 100644
--- a/ipfs-api/src/client.rs
+++ b/ipfs-api/src/client.rs
@@ -10,7 +10,7 @@ use futures::{Future, IntoFuture, stream::{self, Stream}};
use header::TRAILER;
use read::{JsonLineDecoder, LineDecoder, StreamReader};
use request::{self, ApiRequest};
-use response::{self, Error, ErrorKind};
+use response::{self, Error};
use http::uri::InvalidUri;
use hyper::{self, Chunk, Request, Response, StatusCode, Uri, client::{Client, HttpConnector}};
use hyper_multipart::client::multipart;
@@ -98,9 +98,9 @@ impl IpfsClient {
#[inline]
fn build_error_from_body(chunk: Chunk) -> Error {
match serde_json::from_slice(&chunk) {
- Ok(e) => ErrorKind::Api(e).into(),
+ Ok(e) => Error::Api(e).into(),
Err(_) => match String::from_utf8(chunk.to_vec()) {
- Ok(s) => ErrorKind::Uncategorized(s).into(),
+ Ok(s) => Error::Uncategorized(s).into(),
Err(e) => e.into(),
},
}
@@ -290,7 +290,7 @@ impl IpfsClient {
if trailer == "X-Stream-Error" {
true
} else {
- let err = ErrorKind::UnrecognizedTrailerHeader(
+ let err = Error::UnrecognizedTrailerHeader(
String::from_utf8_lossy(trailer.as_ref()).into(),
);
diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs
index 05fe0fa..8c7f74f 100644
--- a/ipfs-api/src/lib.rs
+++ b/ipfs-api/src/lib.rs
@@ -84,7 +84,7 @@
extern crate bytes;
#[macro_use]
-extern crate error_chain;
+extern crate failure;
extern crate futures;
extern crate http;
extern crate hyper;
diff --git a/ipfs-api/src/read.rs b/ipfs-api/src/read.rs
index c0e3dd7..513a3ac 100644
--- a/ipfs-api/src/read.rs
+++ b/ipfs-api/src/read.rs
@@ -10,7 +10,7 @@ use bytes::BytesMut;
use futures::{Async, Stream};
use header::X_STREAM_ERROR;
use hyper::Chunk;
-use response::{Error, ErrorKind};
+use response::Error;
use serde::Deserialize;
use serde_json;
use std::{cmp, io::{self, Read}, marker::PhantomData};
@@ -67,7 +67,7 @@ where
if self.parse_stream_error {
match slice.iter().position(|&x| x == b':') {
Some(colon) if &slice[..colon] == X_STREAM_ERROR.as_bytes() => {
- let e = ErrorKind::StreamError(
+ let e = Error::StreamError(
String::from_utf8_lossy(&slice[colon + 2..]).into(),
);
@@ -187,7 +187,7 @@ where
// Stream could not be read from.
//
Ok(Async::NotReady) => return Err(io::ErrorKind::WouldBlock.into()),
- Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e.description())),
+ Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e.to_string())),
}
}
}
diff --git a/ipfs-api/src/response/error.rs b/ipfs-api/src/response/error.rs
index 4cdd1c1..474275c 100644
--- a/ipfs-api/src/response/error.rs
+++ b/ipfs-api/src/response/error.rs
@@ -11,50 +11,84 @@ use hyper;
use serde_json;
use serde_urlencoded;
use std::string::FromUtf8Error;
+use std;
-#[derive(Debug, Deserialize)]
+#[derive(Fail, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
+#[fail(display = "{}", message)]
pub struct ApiError {
pub message: String,
pub code: u8,
}
-error_chain! {
- foreign_links {
- Client(hyper::Error);
- Http(http::Error);
- Parse(serde_json::Error);
- ParseUtf8(FromUtf8Error);
- Url(http::uri::InvalidUri);
- Io(::std::io::Error);
- EncodeUrl(serde_urlencoded::ser::Error);
+#[derive(Fail, Debug)]
+pub enum Error {
+ // Foreign errors.
+ #[fail(display = "hyper client error '{}'", _0)]
+ Client(hyper::Error),
+ #[fail(display = "http error '{}'", _0)]
+ Http(http::Error),
+ #[fail(display = "json parse error '{}'", _0)]
+ Parse(serde_json::Error),
+ #[fail(display = "utf8 decoding error '{}'", _0)]
+ ParseUtf8(FromUtf8Error),
+ #[fail(display = "uri error '{}'", _0)]
+ Url(http::uri::InvalidUri),
+ #[fail(display = "io error '{}'", _0)]
+ Io(std::io::Error),
+ #[fail(display = "url encoding error '{}'", _0)]
+ EncodeUrl(serde_urlencoded::ser::Error),
+ /// An error returned by the Ipfs api.
+ #[fail(display = "api returned error '{}'", _0)]
+ Api(ApiError),
+ /// A stream error indicated in the Trailer header.
+ #[fail(display = "api returned an error while streaming: '{}'", _0)]
+ StreamError(String),
+ /// API returned a trailer header with unrecognized value.
+ #[fail(display = "api returned a trailer header with unknown value: '{}'", _0)]
+ UnrecognizedTrailerHeader(String),
+ #[fail(display = "api returned unknwon error '{}'", _0)]
+ Uncategorized(String),
+}
+
+impl From<hyper::Error> for Error {
+ fn from(err: hyper::Error) -> Error {
+ Error::Client(err)
}
+}
- errors {
- /// An error returned by the Ipfs api.
- ///
- Api(err: ApiError) {
- description("api returned an error"),
- display("api returned '{}'", err.message)
- }
+impl From<http::Error> for Error {
+ fn from(err: http::Error) -> Error {
+ Error::Http(err)
+ }
+}
- /// A stream error indicated in the Trailer header.
- ///
- StreamError(err: String) {
- description("api returned a stream error"),
- display("api returned an error while streaming: '{}'", err)
- }
+impl From<serde_json::Error> for Error {
+ fn from(err: serde_json::Error) -> Error {
+ Error::Parse(err)
+ }
+}
- /// API returned a trailer header with unrecognized value.
- ///
- UnrecognizedTrailerHeader(value: String) {
- description("api returned a trailer header with an unknown value"),
- display("api returned a trailer header with value: '{}'", value)
- }
-
- Uncategorized(err: String) {
- description("api returned an unknown error"),
- display("api returned '{}'", err)
- }
+impl From<FromUtf8Error> for Error {
+ fn from(err: FromUtf8Error) -> Error {
+ Error::ParseUtf8(err)
+ }
+}
+
+impl From<http::uri::InvalidUri> for Error {
+ fn from(err: http::uri::InvalidUri) -> Error {
+ Error::Url(err)
+ }
+}
+
+impl From<std::io::Error> for Error {
+ fn from(err: std::io::Error) -> Error {
+ Error::Io(err)
+ }
+}
+
+impl From<serde_urlencoded::ser::Error> for Error {
+ fn from(err: serde_urlencoded::ser::Error) -> Error {
+ Error::EncodeUrl(err)
}
}