summaryrefslogtreecommitdiffstats
path: root/ipfs-api/src/response/error.rs
blob: 5287a3d107961ea596a00b98963b4e531ed427ad (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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 crate::serde::Deserialize;

#[cfg(feature = "actix")]
use awc;
use http;
#[cfg(feature = "hyper")]
use hyper;
use serde_json;
use serde_urlencoded;
use std;
use std::string::FromUtf8Error;

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

#[cfg_attr(feature = "actix", derive(Display))]
#[cfg_attr(feature = "hyper", derive(Fail))]
#[derive(Debug)]
pub enum Error {
    /// Foreign errors.
    #[cfg(feature = "hyper")]
    #[cfg_attr(feature = "hyper", fail(display = "hyper client error '{}'", _0))]
    Client(hyper::Error),

    #[cfg(feature = "actix")]
    #[cfg_attr(
        feature = "actix",
        display(fmt = "actix client payload error '{}'", _0)
    )]
    ClientPayload(awc::error::PayloadError),

    #[cfg(feature = "actix")]
    #[cfg_attr(
        feature = "actix",
        display(fmt = "actix client send request error '{}'", _0)
    )]
    ClientSend(awc::error::SendRequestError),

    #[cfg_attr(feature = "actix", display(fmt = "http error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "http error '{}'", _0))]
    Http(http::Error),

    #[cfg_attr(feature = "actix", display(fmt = "json parse error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "json parse error '{}'", _0))]
    Parse(serde_json::Error),

    #[cfg_attr(feature = "actix", display(fmt = "utf8 decoding error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "utf8 decoding error '{}'", _0))]
    ParseUtf8(FromUtf8Error),

    #[cfg_attr(feature = "actix", display(fmt = "uri error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "uri error '{}'", _0))]
    Url(http::uri::InvalidUri),

    #[cfg_attr(feature = "actix", display(fmt = "io error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "io error '{}'", _0))]
    Io(std::io::Error),

    #[cfg_attr(feature = "actix", display(fmt = "url encoding error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "url encoding error '{}'", _0))]
    EncodeUrl(serde_urlencoded::ser::Error),

    /// An error returned by the Ipfs api.
    #[cfg_attr(feature = "actix", display(fmt = "api returned error '{}'", _0))]
    #[cfg_attr(feature = "hyper", fail(display = "api returned error '{}'", _0))]
    Api(ApiError),

    /// A stream error indicated in the Trailer header.
    #[cfg_attr(
        feature = "actix",
        display(fmt = "api returned an error while streaming: '{}'", _0)
    )]
    #[cfg_attr(
        feature = "hyper",
        fail(display = "api returned an error while streaming: '{}'", _0)
    )]
    StreamError(String),

    /// API returned a trailer header with unrecognized value.
    #[cfg_attr(
        feature = "actix",
        display(fmt = "api returned a trailer header with unknown value: '{}'", _0)
    )]
    #[cfg_attr(
        feature = "hyper",
        fail(display = "api returned a trailer header with unknown value: '{}'", _0)
    )]
    UnrecognizedTrailerHeader(String),

    #[cfg_attr(
        feature = "actix",
        display(fmt = "api returned unknwon error '{}'", _0)
    )]
    #[cfg_attr(
        feature = "hyper",
        fail(display = "api returned unknwon error '{}'", _0)
    )]
    Uncategorized(String),
}

#[cfg(feature = "hyper")]
impl From<hyper::Error> for Error {
    fn from(err: hyper::Error) -> Error {
        Error::Client(err)
    }
}

#[cfg(feature = "actix")]
impl From<awc::error::SendRequestError> for Error {
    fn from(err: awc::error::SendRequestError) -> Error {
        Error::ClientSend(err)
    }
}

#[cfg(feature = "actix")]
impl From<awc::error::PayloadError> for Error {
    fn from(err: awc::error::PayloadError) -> Error {
        Error::ClientPayload(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)
    }
}