2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-14 19:30:37 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-02-14 19:30:37 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-14 19:30:37 +01:00
|
|
|
|
2017-03-22 07:02:14 +01:00
|
|
|
use {multihash, cid, http};
|
2017-02-24 10:32:42 +01:00
|
|
|
use route::Out;
|
2017-02-14 19:30:37 +01:00
|
|
|
|
|
|
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
/// IPFS server error
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ServerError {
|
|
|
|
/// Wrapped `std::io::Error`
|
|
|
|
IoError(::std::io::Error),
|
|
|
|
/// Other `hyper` error
|
2017-03-22 07:02:14 +01:00
|
|
|
Other(http::hyper::error::Error),
|
2017-02-24 10:32:42 +01:00
|
|
|
/// Invalid --ipfs-api-interface
|
|
|
|
InvalidInterface
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
/// Handle IO errors (ports taken when starting the server).
|
|
|
|
impl From<::std::io::Error> for ServerError {
|
|
|
|
fn from(err: ::std::io::Error) -> ServerError {
|
|
|
|
ServerError::IoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<http::hyper::error::Error> for ServerError {
|
|
|
|
fn from(err: http::hyper::error::Error) -> ServerError {
|
|
|
|
ServerError::Other(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ServerError> for String {
|
|
|
|
fn from(err: ServerError) -> String {
|
|
|
|
match err {
|
|
|
|
ServerError::IoError(err) => err.to_string(),
|
|
|
|
ServerError::Other(err) => err.to_string(),
|
|
|
|
ServerError::InvalidInterface => "Invalid --ipfs-api-interface parameter".into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::fmt::Display for ServerError {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ServerError::IoError(err) => write!(f, "Io Error: {}", err),
|
|
|
|
ServerError::Other(err) => write!(f, "Other error: {}", err),
|
|
|
|
ServerError::InvalidInterface => write!(f, "Invalid interface"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::error::Error for ServerError {}
|
|
|
|
|
2017-02-15 18:07:30 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
2017-02-14 19:30:37 +01:00
|
|
|
pub enum Error {
|
|
|
|
CidParsingFailed,
|
|
|
|
UnsupportedHash,
|
|
|
|
UnsupportedCid,
|
|
|
|
BlockNotFound,
|
|
|
|
TransactionNotFound,
|
|
|
|
StateRootNotFound,
|
2017-02-16 18:39:13 +01:00
|
|
|
ContractNotFound,
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 18:07:30 +01:00
|
|
|
/// Convert Error into Out, handy when switching from Rust's Result-based
|
|
|
|
/// error handling to Hyper's request handling.
|
2017-02-14 19:30:37 +01:00
|
|
|
impl From<Error> for Out {
|
|
|
|
fn from(err: Error) -> Out {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match err {
|
|
|
|
UnsupportedHash => Out::Bad("Hash must be Keccak-256"),
|
|
|
|
UnsupportedCid => Out::Bad("CID codec not supported"),
|
|
|
|
CidParsingFailed => Out::Bad("CID parsing failed"),
|
|
|
|
BlockNotFound => Out::NotFound("Block not found"),
|
|
|
|
TransactionNotFound => Out::NotFound("Transaction not found"),
|
|
|
|
StateRootNotFound => Out::NotFound("State root not found"),
|
2017-02-16 18:39:13 +01:00
|
|
|
ContractNotFound => Out::NotFound("Contract not found"),
|
2017-02-14 19:30:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:07:30 +01:00
|
|
|
/// Convert Content ID errors.
|
2017-02-14 19:30:37 +01:00
|
|
|
impl From<cid::Error> for Error {
|
|
|
|
fn from(_: cid::Error) -> Error {
|
|
|
|
Error::CidParsingFailed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:07:30 +01:00
|
|
|
/// Convert multihash errors (multihash being part of CID).
|
2017-02-14 19:30:37 +01:00
|
|
|
impl From<multihash::Error> for Error {
|
|
|
|
fn from(_: multihash::Error) -> Error {
|
|
|
|
Error::CidParsingFailed
|
|
|
|
}
|
2019-01-07 11:33:07 +01:00
|
|
|
}
|