summaryrefslogtreecommitdiffstats
path: root/ipfs-api/src/response/error.rs
blob: 474275ca3c20788504304455d4519d897fef675c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2017 rust-ipfs-api Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//

use http;
use hyper;
use serde_json;
use serde_urlencoded;
use std::string::FromUtf8Error;
use std;

#[derive(Fail, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
#[fail(display = "{}", message)]
pub struct ApiError {
    pub message: String,
    pub code: u8,
}

#[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)
    }
}

impl From<http::Error> for Error {
    fn from(err: http::Error) -> Error {
        Error::Http(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::Parse(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)
    }
}