diff options
Diffstat (limited to 'ipfs-api/src/response/error.rs')
-rw-r--r-- | ipfs-api/src/response/error.rs | 102 |
1 files changed, 68 insertions, 34 deletions
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) } } |