proxmox-backup/proxmox-rest-server/src/formatter.rs

171 lines
4.8 KiB
Rust
Raw Normal View History

//! Helpers to format response data
use std::collections::HashMap;
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::header;
use hyper::{Body, Response, StatusCode};
2018-12-05 17:22:56 +00:00
use proxmox_router::{HttpError, RpcEnvironment};
use proxmox_schema::ParameterError;
/// Extension to set error message for server side logging
pub(crate) struct ErrorMessageExtension(pub String);
/// Methods to format data and errors
pub trait OutputFormatter: Send + Sync {
/// Transform json data into a http response
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body>;
/// Transform errors into a http response
fn format_error(&self, err: Error) -> Response<Body>;
/// Transform a [Result] into a http response
fn format_result(
&self,
result: Result<Value, Error>,
rpcenv: &dyn RpcEnvironment,
) -> Response<Body> {
match result {
Ok(data) => self.format_data(data, rpcenv),
Err(err) => self.format_error(err),
}
}
2018-12-05 11:42:25 +00:00
}
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
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
}
}
struct JsonFormatter();
/// Format data as ``application/json``
///
/// The returned json object contains the following properties:
///
/// * ``data``: The result data (on success)
///
/// Any result attributes set on ``rpcenv`` are also added to the object.
///
/// Errors generates a BAD_REQUEST containing the error
/// message as string.
pub static JSON_FORMATTER: &'static dyn OutputFormatter = &JsonFormatter();
impl OutputFormatter for JsonFormatter {
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({ "data": data });
add_result_attributes(&mut result, rpcenv);
json_data_response(result)
}
2018-12-05 11:42:25 +00:00
fn format_error(&self, 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
};
2018-12-05 11:42:25 +00:00
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(JSON_CONTENT_TYPE),
);
2018-12-05 11:42:25 +00:00
response
.extensions_mut()
.insert(ErrorMessageExtension(err.to_string()));
2018-12-05 17:22:56 +00:00
response
}
}
2018-12-05 11:42:25 +00:00
/// Format data as ExtJS compatible ``application/json``
///
/// The returned json object contains the following properties:
///
/// * ``success``: boolean attribute indicating the success.
///
/// * ``data``: The result data (on success)
///
/// * ``message``: The error message (on failure)
///
/// * ``errors``: detailed list of errors (if available)
///
/// Any result attributes set on ``rpcenv`` are also added to the object.
///
/// Please note that errors return status code OK, but setting success
/// to false.
pub static EXTJS_FORMATTER: &'static dyn OutputFormatter = &ExtJsFormatter();
struct ExtJsFormatter();
impl OutputFormatter for ExtJsFormatter {
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({
"data": data,
"success": true
});
add_result_attributes(&mut result, rpcenv);
json_data_response(result)
}
2018-12-05 11:42:25 +00:00
fn format_error(&self, err: Error) -> Response<Body> {
let mut errors = HashMap::new();
2018-12-05 11:42:25 +00:00
let message: String = match err.downcast::<ParameterError>() {
Ok(param_err) => {
for (name, err) in param_err {
errors.insert(name, err.to_string());
}
String::from("parameter verification errors")
}
Err(err) => err.to_string(),
};
2018-12-05 11:42:25 +00:00
let result = json!({
"message": message,
"errors": errors,
"success": false
});
let mut response = json_data_response(result);
response
.extensions_mut()
.insert(ErrorMessageExtension(message));
2018-12-05 11:42:25 +00:00
response
}
}