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 780FC9E167 for ; Mon, 27 Nov 2023 10:09:26 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 61A5A2D71 for ; Mon, 27 Nov 2023 10:09:26 +0100 (CET) Received: from druiddev.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP for ; Mon, 27 Nov 2023 10:09:25 +0100 (CET) Received: by druiddev.proxmox.com (Postfix, from userid 1000) id CCE478D2B8; Mon, 27 Nov 2023 10:09:25 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Mon, 27 Nov 2023 10:09:17 +0100 Message-Id: <20231127090917.2871106-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.525 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 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [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:09:26 -0000 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