Skip to content

Commit

Permalink
rpc: slightly better HTTP error handling
Browse files Browse the repository at this point in the history
Adds a bogus error code for low-level HTTP errors, and at least captures
the error message from the `http` crate or `hyper`.
  • Loading branch information
tony-iqlusion committed Dec 11, 2019
1 parent ead8a62 commit a50b503
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tendermint/src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl Error {
}
}

/// Create a low-level HTTP error
pub fn http_error(message: impl Into<String>) -> Error {
Error {
code: Code::HttpError,
message: message.into(),
data: None,
}
}

/// Create a new invalid parameter error
pub fn invalid_params(data: &str) -> Error {
Error::new(Code::InvalidParams, Some(data.to_string()))
Expand Down Expand Up @@ -94,15 +103,13 @@ impl Fail for Error {

impl From<http::Error> for Error {
fn from(http_error: http::Error) -> Error {
// TODO(tarcieri): properly handle HTTP errors
panic!("what am I supposed to do with this? {:?}", http_error);
Error::http_error(http_error.to_string())
}
}

impl From<hyper::Error> for Error {
fn from(hyper_error: hyper::Error) -> Error {
// TODO(tarcieri): properly handle HTTP errors
panic!("what am I supposed to do with this? {:?}", hyper_error);
Error::http_error(hyper_error.to_string())
}
}

Expand All @@ -112,6 +119,10 @@ impl From<hyper::Error> for Error {
/// <https://github.com/tendermint/tendermint/blob/master/rpc/lib/types/types.go>
#[derive(Copy, Clone, Debug, Eq, Fail, Hash, PartialEq, PartialOrd, Ord)]
pub enum Code {
/// Low-level HTTP error
#[fail(display = "HTTP error")]
HttpError,

/// Parse error i.e. invalid JSON (-32700)
#[fail(display = "Parse error. Invalid JSON")]
ParseError,
Expand Down Expand Up @@ -151,6 +162,7 @@ impl Code {
impl From<i32> for Code {
fn from(value: i32) -> Code {
match value {
0 => Code::HttpError,
-32700 => Code::ParseError,
-32600 => Code::InvalidRequest,
-32601 => Code::MethodNotFound,
Expand All @@ -165,6 +177,7 @@ impl From<i32> for Code {
impl From<Code> for i32 {
fn from(code: Code) -> i32 {
match code {
Code::HttpError => 0,
Code::ParseError => -32700,
Code::InvalidRequest => -32600,
Code::MethodNotFound => -32601,
Expand Down

0 comments on commit a50b503

Please sign in to comment.