mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 08:51:08 +03:00
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:
parent
db7b686073
commit
7db8e5e2dd
2 changed files with 11 additions and 10 deletions
|
@ -743,11 +743,11 @@ impl PoolPushHandler {
|
|||
parse_body(req)
|
||||
.and_then(move |wrapper: TxWrapper| {
|
||||
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| {
|
||||
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| {
|
||||
let source = pool::TxSource {
|
||||
|
@ -770,7 +770,7 @@ impl PoolPushHandler {
|
|||
.add_to_pool(source, tx, !fluff, &header.hash())
|
||||
.map_err(|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(
|
||||
req.into_body()
|
||||
.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()[..]) {
|
||||
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())
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -566,7 +566,7 @@ fn create_error_response(e: Error) -> Response<Body> {
|
|||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.header("access-control-allow-origin", "*")
|
||||
.header("access-control-allow-headers", "Content-Type")
|
||||
.body(format!("{}", e.kind()).into())
|
||||
.body(format!("{}", e).into())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
|
@ -584,9 +584,6 @@ fn response<T: Into<Body>>(status: StatusCode, text: T) -> Response<Body> {
|
|||
.header("access-control-allow-origin", "*")
|
||||
.body(text.into())
|
||||
.unwrap()
|
||||
//let mut resp = Response::new(text.into());
|
||||
//*resp.status_mut() = status;
|
||||
//resp
|
||||
}
|
||||
|
||||
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())
|
||||
.and_then(|body| match serde_json::from_reader(&body.to_vec()[..]) {
|
||||
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())
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue