proxmox-rest-server: return status code with ExtJsFormatter

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Dietmar Maurer 2023-11-27 10:09:17 +01:00 committed by Wolfgang Bumiller
parent 3932e5bedf
commit 6b59158aaf

View File

@ -166,6 +166,8 @@ pub(crate) fn error_to_response(err: Error) -> Response<Body> {
/// ///
/// * ``success``: boolean attribute indicating the success. /// * ``success``: boolean attribute indicating the success.
/// ///
/// * ``status``: api call status code.
///
/// * ``data``: The result data (on success) /// * ``data``: The result data (on success)
/// ///
/// * ``message``: The error message (on failure) /// * ``message``: The error message (on failure)
@ -174,8 +176,9 @@ pub(crate) fn error_to_response(err: Error) -> Response<Body> {
/// ///
/// Any result attributes set on ``rpcenv`` are also added to the object. /// Any result attributes set on ``rpcenv`` are also added to the object.
/// ///
/// Please note that errors return status code OK, but setting success /// Please note that errors return a HTTP response with status code OK, but setting success
/// to false. /// to false. The real status from the API call is encoded in the status
/// property.
pub static EXTJS_FORMATTER: &'static dyn OutputFormatter = &ExtJsFormatter(); pub static EXTJS_FORMATTER: &'static dyn OutputFormatter = &ExtJsFormatter();
struct ExtJsFormatter(); struct ExtJsFormatter();
@ -184,7 +187,8 @@ impl OutputFormatter for ExtJsFormatter {
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> { fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({ let mut result = json!({
"data": data, "data": data,
"success": true "success": true,
"status": StatusCode::OK.as_u16(),
}); });
add_result_attributes(&mut result, rpcenv); add_result_attributes(&mut result, rpcenv);
@ -199,6 +203,7 @@ impl OutputFormatter for ExtJsFormatter {
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
let mut value = json!({ let mut value = json!({
"success": true, "success": true,
"status": StatusCode::OK.as_u16(),
}); });
add_result_attributes(&mut value, rpcenv); add_result_attributes(&mut value, rpcenv);
@ -212,20 +217,30 @@ impl OutputFormatter for ExtJsFormatter {
fn format_error(&self, err: Error) -> Response<Body> { fn format_error(&self, err: Error) -> Response<Body> {
let mut errors = HashMap::new(); let mut errors = HashMap::new();
let message: String = match err.downcast::<ParameterError>() { let (message, status) = if err.is::<ParameterError>() {
match err.downcast::<ParameterError>() {
Ok(param_err) => { Ok(param_err) => {
for (name, err) in param_err { for (name, err) in param_err {
errors.insert(name, err.to_string()); errors.insert(name, err.to_string());
} }
String::from("parameter verification errors") (String::from("parameter verification errors"), StatusCode::BAD_REQUEST)
} }
Err(err) => err.to_string(), Err(err) => (err.to_string(), StatusCode::BAD_REQUEST),
}
} else {
let status = if let Some(apierr) = err.downcast_ref::<HttpError>() {
apierr.code
} else {
StatusCode::BAD_REQUEST
};
(err.to_string(), status)
}; };
let result = json!({ let result = json!({
"message": message, "message": message,
"errors": errors, "errors": errors,
"success": false "success": false,
"status": status.as_u16(),
}); });
let mut response = json_data_response(result); let mut response = json_data_response(result);