proxmox-backup/src/server/formatter.rs

127 lines
3.0 KiB
Rust
Raw Normal View History

use anyhow::{Error};
2018-12-05 11:42:25 +00:00
use serde_json::{json, Value};
2018-12-05 17:22:56 +00:00
use hyper::{Body, Response, StatusCode};
use hyper::header;
use proxmox::api::{HttpError, RpcEnvironment};
/// 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 {
pub format_data: fn(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body>,
pub format_error: fn(err: Error) -> Response<Body>,
2018-12-05 11:42:25 +00:00
}
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_data_response(data: Value) -> Response<Body> {
let json_str = data.to_string();
let raw = json_str.into_bytes();
2018-12-05 11:42:25 +00:00
let mut response = Response::new(raw.into());
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
2018-12-05 11:42:25 +00:00
response
}
2018-12-05 17:22:56 +00:00
fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment)
{
let attributes = match rpcenv.result_attrib().as_object() {
Some(attr) => attr,
None => return,
};
for (key, value) in attributes {
result[key] = value.clone();
2018-12-05 17:22:56 +00:00
}
}
fn json_format_data(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({
"data": data
});
add_result_attributes(&mut result, rpcenv);
json_data_response(result)
}
pub fn json_error_response(err: Error) -> Response<Body> {
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
};
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
response.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
response
2018-12-05 11:42:25 +00:00
}
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
format_data: json_format_data,
format_error: json_error_response,
2018-12-05 11:42:25 +00:00
};
fn extjs_format_data(data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
2018-12-05 11:42:25 +00:00
let mut result = json!({
"data": data,
"success": true
});
2018-12-05 11:42:25 +00:00
add_result_attributes(&mut result, rpcenv);
2018-12-05 17:22:56 +00:00
json_data_response(result)
}
2018-12-05 11:42:25 +00:00
fn extjs_format_error(err: Error) -> Response<Body> {
2018-12-05 11:42:25 +00:00
let mut errors = vec![];
let message = err.to_string();
errors.push(&message);
2018-12-05 11:42:25 +00:00
let result = json!({
"message": message,
"errors": errors,
"success": false
});
2018-12-05 11:42:25 +00:00
let mut response = json_data_response(result);
response.extensions_mut().insert(ErrorMessageExtension(message));
response
2018-12-05 11:42:25 +00:00
}
pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
format_data: extjs_format_data,
format_error: extjs_format_error,
2018-12-05 11:42:25 +00:00
};