diff --git a/src/api_schema/router.rs b/src/api_schema/router.rs index 60d00e4c..09651d59 100644 --- a/src/api_schema/router.rs +++ b/src/api_schema/router.rs @@ -62,7 +62,7 @@ impl HttpError { impl fmt::Display for HttpError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Error {}: {}", self.code, self.message) + write!(f, "{}", self.message) } } diff --git a/src/server/formatter.rs b/src/server/formatter.rs index a4b89888..b7ad5a65 100644 --- a/src/server/formatter.rs +++ b/src/server/formatter.rs @@ -1,7 +1,7 @@ use failure::*; use serde_json::{json, Value}; -use crate::api_schema::router::RpcEnvironment; +use crate::api_schema::router::{HttpError, RpcEnvironment}; use hyper::{Body, Response, StatusCode}; use hyper::header; @@ -52,11 +52,19 @@ fn json_format_result(data: Value, rpcenv: &RpcEnvironment) -> Response { fn json_format_error(err: Error) -> Response { - let mut response = Response::new(Body::from(err.to_string())); + let mut response = if let Some(apierr) = err.downcast_ref::() { + let mut resp = Response::new(Body::from(apierr.message.clone())); + *resp.status_mut() = apierr.code; + resp + } else { + let mut resp = Response::new(Body::from(err.to_string())); + *resp.status_mut() = StatusCode::BAD_REQUEST; + resp + }; + response.headers_mut().insert( header::CONTENT_TYPE, header::HeaderValue::from_static(JSON_CONTENT_TYPE)); - *response.status_mut() = StatusCode::BAD_REQUEST; response.extensions_mut().insert(ErrorMessageExtension(err.to_string())); diff --git a/src/server/rest.rs b/src/server/rest.rs index 9c236d82..2a1546ab 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -454,10 +454,10 @@ fn check_auth(method: &hyper::Method, ticket: Option, token: Option