Improve API errors (#1543)

Address ##1525 in particular and improve error messages in general.
Instead of `Request Error: Error { inner:` a client would get:
`Generic error: Invalid request body: missing field `method` at line 1 column 162`
This commit is contained in:
hashmap 2018-09-19 00:11:58 +02:00 committed by Ignotus Peverell
parent db7b686073
commit 7db8e5e2dd
2 changed files with 11 additions and 10 deletions

View file

@ -743,11 +743,11 @@ impl PoolPushHandler {
parse_body(req) parse_body(req)
.and_then(move |wrapper: TxWrapper| { .and_then(move |wrapper: TxWrapper| {
util::from_hex(wrapper.tx_hex) util::from_hex(wrapper.tx_hex)
.map_err(|_| ErrorKind::RequestError("Bad request".to_owned()).into()) .map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)).into())
}) })
.and_then(move |tx_bin| { .and_then(move |tx_bin| {
ser::deserialize(&mut &tx_bin[..]) ser::deserialize(&mut &tx_bin[..])
.map_err(|_| ErrorKind::RequestError("Bad request".to_owned()).into()) .map_err(|e| ErrorKind::RequestError(format!("Bad request: {}", e)).into())
}) })
.and_then(move |tx: Transaction| { .and_then(move |tx: Transaction| {
let source = pool::TxSource { let source = pool::TxSource {
@ -770,7 +770,7 @@ impl PoolPushHandler {
.add_to_pool(source, tx, !fluff, &header.hash()) .add_to_pool(source, tx, !fluff, &header.hash())
.map_err(|e| { .map_err(|e| {
error!(LOGGER, "update_pool: failed with error: {:?}", e); error!(LOGGER, "update_pool: failed with error: {:?}", e);
ErrorKind::RequestError("Bad request".to_owned()).into() ErrorKind::Internal(format!("Failed to update pool: {:?}", e)).into()
}) })
}), }),
) )
@ -897,10 +897,12 @@ where
Box::new( Box::new(
req.into_body() req.into_body()
.concat2() .concat2()
.map_err(|_e| ErrorKind::RequestError("Failed to read request".to_owned()).into()) .map_err(|e| ErrorKind::RequestError(format!("Failed to read request: {}", e)).into())
.and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) { .and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) {
Ok(obj) => ok(obj), Ok(obj) => ok(obj),
Err(_) => err(ErrorKind::RequestError("Invalid request body".to_owned()).into()), Err(e) => {
err(ErrorKind::RequestError(format!("Invalid request body: {}", e)).into())
}
}), }),
) )
} }

View file

@ -566,7 +566,7 @@ fn create_error_response(e: Error) -> Response<Body> {
.status(StatusCode::INTERNAL_SERVER_ERROR) .status(StatusCode::INTERNAL_SERVER_ERROR)
.header("access-control-allow-origin", "*") .header("access-control-allow-origin", "*")
.header("access-control-allow-headers", "Content-Type") .header("access-control-allow-headers", "Content-Type")
.body(format!("{}", e.kind()).into()) .body(format!("{}", e).into())
.unwrap() .unwrap()
} }
@ -584,9 +584,6 @@ fn response<T: Into<Body>>(status: StatusCode, text: T) -> Response<Body> {
.header("access-control-allow-origin", "*") .header("access-control-allow-origin", "*")
.body(text.into()) .body(text.into())
.unwrap() .unwrap()
//let mut resp = Response::new(text.into());
//*resp.status_mut() = status;
//resp
} }
fn parse_params(req: &Request<Body>) -> HashMap<String, Vec<String>> { fn parse_params(req: &Request<Body>) -> HashMap<String, Vec<String>> {
@ -615,7 +612,9 @@ where
.map_err(|_| ErrorKind::GenericError("Failed to read request".to_owned()).into()) .map_err(|_| ErrorKind::GenericError("Failed to read request".to_owned()).into())
.and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) { .and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) {
Ok(obj) => ok(obj), Ok(obj) => ok(obj),
Err(_) => err(ErrorKind::GenericError("Invalid request body".to_owned()).into()), Err(e) => {
err(ErrorKind::GenericError(format!("Invalid request body: {}", e)).into())
}
}), }),
) )
} }