src/server/formatter.rs: further cleanups and renaming ...
This commit is contained in:
parent
d55037e471
commit
78b5191550
|
@ -83,14 +83,9 @@ fn upload_chunk(
|
||||||
let abort_future = env.worker.abort_future().then(|_| Ok(Value::Null));
|
let abort_future = env.worker.abort_future().then(|_| Ok(Value::Null));
|
||||||
|
|
||||||
let resp = upload.select(abort_future)
|
let resp = upload.select(abort_future)
|
||||||
.then(move |result| {
|
.and_then(|(result, _)| Ok(result))
|
||||||
use crate::server::formatter::*;
|
.map_err(|(err, _)| err)
|
||||||
match result {
|
.then(|res| Ok(crate::server::formatter::json_response(res)));
|
||||||
Ok((result,_)) => Ok(json_response(result)),
|
|
||||||
Err((err, _)) => Ok(json_format_error(err)),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
Ok(Box::new(resp))
|
Ok(Box::new(resp))
|
||||||
|
|
||||||
|
|
|
@ -18,10 +18,16 @@ pub struct OutputFormatter {
|
||||||
|
|
||||||
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
|
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
|
||||||
|
|
||||||
|
pub fn json_response(result: Result<Value, Error>) -> Response<Body> {
|
||||||
|
match result {
|
||||||
|
Ok(data) => json_data_response(data),
|
||||||
|
Err(err) => json_error_response(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn json_response(result: Value) -> Response<Body> {
|
pub fn json_data_response(data: Value) -> Response<Body> {
|
||||||
|
|
||||||
let json_str = result.to_string();
|
let json_str = data.to_string();
|
||||||
|
|
||||||
let raw = json_str.into_bytes();
|
let raw = json_str.into_bytes();
|
||||||
|
|
||||||
|
@ -47,10 +53,10 @@ fn json_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
||||||
result["changes"] = changes.clone();
|
result["changes"] = changes.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
json_response(result)
|
json_data_response(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn json_format_error(err: Error) -> Response<Body> {
|
pub fn json_error_response(err: Error) -> Response<Body> {
|
||||||
|
|
||||||
let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
||||||
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
||||||
|
@ -73,7 +79,7 @@ pub fn json_format_error(err: Error) -> Response<Body> {
|
||||||
|
|
||||||
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
|
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
|
||||||
format_data: json_format_data,
|
format_data: json_format_data,
|
||||||
format_error: json_format_error,
|
format_error: json_error_response,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn extjs_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
fn extjs_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
||||||
|
@ -92,7 +98,7 @@ fn extjs_format_data(data: Value, rpcenv: &RpcEnvironment) -> Response<Body> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
json_response(result)
|
json_data_response(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extjs_format_error(err: Error) -> Response<Body> {
|
fn extjs_format_error(err: Error) -> Response<Body> {
|
||||||
|
@ -108,7 +114,7 @@ fn extjs_format_error(err: Error) -> Response<Body> {
|
||||||
"success": false
|
"success": false
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut response = json_response(result);
|
let mut response = json_data_response(result);
|
||||||
|
|
||||||
response.extensions_mut().insert(ErrorMessageExtension(message));
|
response.extensions_mut().insert(ErrorMessageExtension(message));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue