From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 22A001FF13C for ; Thu, 30 Apr 2026 14:49:49 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 06B8176F8; Thu, 30 Apr 2026 14:49:49 +0200 (CEST) From: Christoph Heiss To: pdm-devel@lists.proxmox.com Subject: [PATCH installer v4 27/40] common: http: retrieve error message from body on post() Date: Thu, 30 Apr 2026 14:46:56 +0200 Message-ID: <20260430124712.1614305-28-c.heiss@proxmox.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260430124712.1614305-1-c.heiss@proxmox.com> References: <20260430124712.1614305-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1777553284652 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.075 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 Message-ID-Hash: OBPDFKTYXW5R5CO4NYXGNCATUWJMTVUI X-Message-ID-Hash: OBPDFKTYXW5R5CO4NYXGNCATUWJMTVUI X-MailFrom: c.heiss@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: ureq does not include that by default in its own error type. It's useful to display to the user, to better pinpoint any problems. Signed-off-by: Christoph Heiss --- Changes v3 -> v4: * new patch proxmox-installer-common/src/http.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/proxmox-installer-common/src/http.rs b/proxmox-installer-common/src/http.rs index 445d6c4..ca64a73 100644 --- a/proxmox-installer-common/src/http.rs +++ b/proxmox-installer-common/src/http.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Result, bail}; use rustls::pki_types::{CertificateDer, ServerName, UnixTime}; use rustls::{ClientConfig, ClientConnection, StreamOwned}; use sha2::{Digest, Sha256}; @@ -8,6 +8,7 @@ use std::str::FromStr; use std::sync::Arc; use std::time::Duration; use ureq::Agent; +use ureq::http::StatusCode; use ureq::unversioned::resolver::DefaultResolver; use ureq::unversioned::transport::{ Buffers, ConnectionDetails, Connector, Either, LazyBuffers, NextTimeout, TcpConnector, @@ -142,7 +143,8 @@ pub struct Response { /// /// # Returns /// -/// A [`Response`] holding the body contents and the `Content-Type` header, if present. +/// If the request was successful with 200 OK or 201 CREATED, a [`Response`] holding the body +/// contents and the `Content-Type` header, if present. pub fn post( url: &str, fingerprint: Option<&str>, @@ -153,7 +155,12 @@ pub fn post( let mut request = build_agent(fingerprint)? .post(url) - .header("Content-Type", "application/json; charset=utf-8"); + .header("Content-Type", "application/json; charset=utf-8") + .config() + // don't treat 4xx and 5xx statuses as error, so we can extract the + // error message from the body + .http_status_as_error(false) + .build(); for (name, value) in headers.iter() { request = request.header(name, value); @@ -161,6 +168,7 @@ pub fn post( let mut response = request.send(&payload)?; + let body = response.body_mut().read_to_string()?; let content_type = response .headers() .get(header::CONTENT_TYPE) @@ -168,10 +176,10 @@ pub fn post( .map(ContentType::from_str) .transpose()?; - Ok(Response { - body: response.body_mut().read_to_string()?, - content_type, - }) + match response.status() { + StatusCode::OK | StatusCode::CREATED => Ok(Response { body, content_type }), + other => bail!("http error: {other}: {body}"), + } } #[derive(Debug)] -- 2.53.0