2018-12-05 11:42:25 +00:00
|
|
|
use failure::*;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
2019-02-17 16:18:44 +00:00
|
|
|
use crate::api_schema::router::{HttpError, RpcEnvironment};
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2018-12-05 17:22:56 +00:00
|
|
|
use hyper::{Body, Response, StatusCode};
|
|
|
|
use hyper::header;
|
|
|
|
|
2019-02-15 08:55:12 +00:00
|
|
|
/// Extension to set error message for server side logging
|
|
|
|
pub struct ErrorMessageExtension(pub String);
|
|
|
|
|
2018-12-05 11:42:25 +00:00
|
|
|
pub struct OutputFormatter {
|
|
|
|
|
2019-05-09 11:15:15 +00:00
|
|
|
pub format_data: fn(data: Value, rpcenv: &RpcEnvironment) -> Response<Body>,
|
2019-01-26 13:50:37 +00:00
|
|
|
|
|
|
|
pub format_error: fn(err: Error) -> Response<Body>,
|
2018-12-05 11:42:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 14:08:02 +00:00
|
|
|
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
pub fn json_response(result: Result<Value, Error>) -> Response<Body> {
|
|
|
|
match result {
|
|
|
|
Ok(data) => json_data_response(data),
|
|
|
|
Err(err) => json_error_response(err),
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
pub fn json_data_response(data: Value) -> Response<Body> {
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
let json_str = data.to_string();
|
2019-01-26 13:50:37 +00:00
|
|
|
|
|
|
|
let raw = json_str.into_bytes();
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let mut response = Response::new(raw.into());
|
|
|
|
response.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
2019-01-26 14:08:02 +00:00
|
|
|
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
response
|
|
|
|
}
|
2018-12-05 17:22:56 +00:00
|
|
|
|
2019-05-09 11:15:15 +00:00
|
|
|
fn json_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
2018-12-05 17:22:56 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let mut result = json!({
|
|
|
|
"data": data
|
|
|
|
});
|
2018-12-05 17:22:56 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) {
|
|
|
|
result["total"] = Value::from(total);
|
|
|
|
}
|
2018-12-05 17:22:56 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
if let Some(changes) = rpcenv.get_result_attrib("changes") {
|
|
|
|
result["changes"] = changes.clone();
|
2018-12-05 17:22:56 +00:00
|
|
|
}
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
json_data_response(result)
|
2019-01-26 13:50:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
pub fn json_error_response(err: Error) -> Response<Body> {
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-02-17 16:18:44 +00:00
|
|
|
let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
response.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
2019-01-26 14:08:02 +00:00
|
|
|
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-02-15 08:55:12 +00:00
|
|
|
response.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
response
|
2018-12-05 11:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
|
2019-05-09 11:15:15 +00:00
|
|
|
format_data: json_format_data,
|
2019-05-09 11:28:26 +00:00
|
|
|
format_error: json_error_response,
|
2018-12-05 11:42:25 +00:00
|
|
|
};
|
|
|
|
|
2019-05-09 11:15:15 +00:00
|
|
|
fn extjs_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let mut result = json!({
|
|
|
|
"data": data,
|
|
|
|
"success": true
|
|
|
|
});
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
if let Some(total) = rpcenv.get_result_attrib("total").and_then(|v| v.as_u64()) {
|
|
|
|
result["total"] = Value::from(total);
|
|
|
|
}
|
2018-12-05 17:22:56 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
if let Some(changes) = rpcenv.get_result_attrib("changes") {
|
|
|
|
result["changes"] = changes.clone();
|
|
|
|
}
|
2018-12-05 17:22:56 +00:00
|
|
|
|
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
json_data_response(result)
|
2019-01-26 13:50:37 +00:00
|
|
|
}
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn extjs_format_error(err: Error) -> Response<Body> {
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let mut errors = vec![];
|
2019-01-28 12:44:48 +00:00
|
|
|
|
|
|
|
let message = err.to_string();
|
|
|
|
errors.push(&message);
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let result = json!({
|
2019-01-28 12:44:48 +00:00
|
|
|
"message": message,
|
2019-01-26 13:50:37 +00:00
|
|
|
"errors": errors,
|
|
|
|
"success": false
|
|
|
|
});
|
2018-12-05 11:42:25 +00:00
|
|
|
|
2019-05-09 11:28:26 +00:00
|
|
|
let mut response = json_data_response(result);
|
2019-02-15 08:55:12 +00:00
|
|
|
|
|
|
|
response.extensions_mut().insert(ErrorMessageExtension(message));
|
|
|
|
|
|
|
|
response
|
2018-12-05 11:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
|
2019-05-09 11:15:15 +00:00
|
|
|
format_data: extjs_format_data,
|
2019-01-26 13:50:37 +00:00
|
|
|
format_error: extjs_format_error,
|
2018-12-05 11:42:25 +00:00
|
|
|
};
|