From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 010DE9E179 for ; Mon, 27 Nov 2023 10:26:30 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D65863215 for ; Mon, 27 Nov 2023 10:26:30 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Mon, 27 Nov 2023 10:26:30 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 0D9D244A27 for ; Mon, 27 Nov 2023 10:26:30 +0100 (CET) Date: Mon, 27 Nov 2023 10:26:29 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Message-ID: <1110989974.224.1701077189145@webmail.proxmox.com> In-Reply-To: <20231127090917.2871106-1-dietmar@proxmox.com> References: <20231127090917.2871106-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v7.10.6-Rev55 X-Originating-Client: open-xchange-appsuite X-SPAM-LEVEL: Spam detection results: 0 AWL 0.374 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [formatter.rs] Subject: Re: [pbs-devel] [PATCH proxmox] proxmox-rest-server: return status code with ExtJsFormatter X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Nov 2023 09:26:31 -0000 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 wrote: > > > Makes it possible to detect things like authentification failure (401). > > Signed-off-by: Dietmar Maurer > --- > 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 { > /// > /// * ``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 { > /// > /// 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 { > 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, 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 { > let mut errors = HashMap::new(); > > - let message: String = match err.downcast::() { > - Ok(param_err) => { > - for (name, err) in param_err { > - errors.insert(name, err.to_string()); > + let (message, status) = if err.is::() { > + match err.downcast::() { > + 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::() { > + 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