From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH proxmox] proxmox-rest-server: return status code with ExtJsFormatter
Date: Mon, 27 Nov 2023 10:26:29 +0100 (CET) [thread overview]
Message-ID: <1110989974.224.1701077189145@webmail.proxmox.com> (raw)
In-Reply-To: <20231127090917.2871106-1-dietmar@proxmox.com>
Answering myself, this is possibly the wrong fix.
Wolfgang think we should return an HTTP error for 401 early. Seems we do that in PVE also.
Will send another patch.
> On 27.11.2023 10:09 CET Dietmar Maurer <dietmar@proxmox.com> wrote:
>
>
> Makes it possible to detect things like authentification failure (401).
>
> Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
> ---
> proxmox-rest-server/src/formatter.rs | 35 ++++++++++++++++++++--------
> 1 file changed, 25 insertions(+), 10 deletions(-)
>
> diff --git a/proxmox-rest-server/src/formatter.rs b/proxmox-rest-server/src/formatter.rs
> index d05ddd9..d19d680 100644
> --- a/proxmox-rest-server/src/formatter.rs
> +++ b/proxmox-rest-server/src/formatter.rs
> @@ -166,6 +166,8 @@ pub(crate) fn error_to_response(err: Error) -> Response<Body> {
> ///
> /// * ``success``: boolean attribute indicating the success.
> ///
> +/// * ``status``: api call status code.
> +///
> /// * ``data``: The result data (on success)
> ///
> /// * ``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.
> ///
> -/// Please note that errors return status code OK, but setting success
> -/// to false.
> +/// Please note that errors return a HTTP response with status code OK, but setting success
> +/// to false. The real status from the API call is encoded in the status
> +/// property.
> pub static EXTJS_FORMATTER: &'static dyn OutputFormatter = &ExtJsFormatter();
>
> struct ExtJsFormatter();
> @@ -184,7 +187,8 @@ impl OutputFormatter for ExtJsFormatter {
> fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
> let mut result = json!({
> "data": data,
> - "success": true
> + "success": true,
> + "status": StatusCode::OK.as_u16(),
> });
>
> add_result_attributes(&mut result, rpcenv);
> @@ -199,6 +203,7 @@ impl OutputFormatter for ExtJsFormatter {
> ) -> Result<Response<Body>, Error> {
> let mut value = json!({
> "success": true,
> + "status": StatusCode::OK.as_u16(),
> });
>
> add_result_attributes(&mut value, rpcenv);
> @@ -212,20 +217,30 @@ impl OutputFormatter for ExtJsFormatter {
> fn format_error(&self, err: Error) -> Response<Body> {
> let mut errors = HashMap::new();
>
> - let message: String = match err.downcast::<ParameterError>() {
> - Ok(param_err) => {
> - for (name, err) in param_err {
> - errors.insert(name, err.to_string());
> + let (message, status) = if err.is::<ParameterError>() {
> + match err.downcast::<ParameterError>() {
> + Ok(param_err) => {
> + for (name, err) in param_err {
> + errors.insert(name, err.to_string());
> + }
> + (String::from("parameter verification errors"), StatusCode::BAD_REQUEST)
> }
> - String::from("parameter verification errors")
> + Err(err) => (err.to_string(), StatusCode::BAD_REQUEST),
> }
> - Err(err) => err.to_string(),
> + } else {
> + let status = if let Some(apierr) = err.downcast_ref::<HttpError>() {
> + apierr.code
> + } else {
> + StatusCode::BAD_REQUEST
> + };
> + (err.to_string(), status)
> };
>
> let result = json!({
> "message": message,
> "errors": errors,
> - "success": false
> + "success": false,
> + "status": status.as_u16(),
> });
>
> let mut response = json_data_response(result);
> --
> 2.39.2
next prev parent reply other threads:[~2023-11-27 9:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-27 9:09 Dietmar Maurer
2023-11-27 9:26 ` Dietmar Maurer [this message]
2023-11-28 10:35 ` [pbs-devel] applied: " Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1110989974.224.1701077189145@webmail.proxmox.com \
--to=dietmar@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.