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 2012E61A7C; Wed, 26 Jul 2023 14:50:15 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 01D996775; Wed, 26 Jul 2023 14:50:15 +0200 (CEST) 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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS; Wed, 26 Jul 2023 14:50:14 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 1FB3945C09; Wed, 26 Jul 2023 14:50:14 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Cc: Lukas Wagner , Wolfgang Bumiller Date: Wed, 26 Jul 2023 14:50:02 +0200 Message-Id: <20230726125006.616124-2-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230726125006.616124-1-l.wagner@proxmox.com> References: <20230726125006.616124-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.057 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. [lib.rs] Subject: [pve-devel] [PATCH proxmox 1/5] http-error: add new http-error crate X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Jul 2023 12:50:15 -0000 Break out proxmox-router's HttpError into it's own crate so that it can be used without pulling in proxmox-router. This commit also implements `Serialize` for `HttpError` so that it can be returned from perlmod bindings, allowing Perl code to access the status code as well as the message. Also add some smoke-tests to make sure that the `http_bail` and `http_err` macros actually produce valid code. Suggested-by: Wolfgang Bumiller Signed-off-by: Lukas Wagner --- Cargo.toml | 2 + proxmox-http-error/Cargo.toml | 16 +++++++ proxmox-http-error/src/lib.rs | 81 +++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 proxmox-http-error/Cargo.toml create mode 100644 proxmox-http-error/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 54be5f6..cb82253 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "proxmox-borrow", "proxmox-compression", "proxmox-http", + "proxmox-http-error", "proxmox-human-byte", "proxmox-io", "proxmox-lang", @@ -88,6 +89,7 @@ proxmox-api-macro = { version = "1.0.4", path = "proxmox-api-macro" } proxmox-async = { version = "0.4.1", path = "proxmox-async" } proxmox-compression = { version = "0.2.0", path = "proxmox-compression" } proxmox-http = { version = "0.9.0", path = "proxmox-http" } +proxmox-http-error = { version = "0.1.0", path = "proxmox-http-error" } proxmox-human-byte = { version = "0.1.0", path = "proxmox-human-byte" } proxmox-io = { version = "1.0.0", path = "proxmox-io" } proxmox-lang = { version = "1.1", path = "proxmox-lang" } diff --git a/proxmox-http-error/Cargo.toml b/proxmox-http-error/Cargo.toml new file mode 100644 index 0000000..17be2e0 --- /dev/null +++ b/proxmox-http-error/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "proxmox-http-error" +version = "0.1.0" + +edition.workspace = true +authors.workspace = true +license.workspace = true +repository.workspace = true +description = "Proxmox HTTP Error" + +exclude.workspace = true + +[dependencies] +anyhow.workspace = true +http.workspace = true +serde = { workspace = true, features = ["derive"]} diff --git a/proxmox-http-error/src/lib.rs b/proxmox-http-error/src/lib.rs new file mode 100644 index 0000000..de45cb9 --- /dev/null +++ b/proxmox-http-error/src/lib.rs @@ -0,0 +1,81 @@ +use serde::{ser::SerializeStruct, Serialize, Serializer}; +use std::fmt; + +#[doc(hidden)] +pub use http::StatusCode; + +/// HTTP error including `StatusCode` and message. +#[derive(Debug)] +pub struct HttpError { + pub code: StatusCode, + pub message: String, +} + +impl std::error::Error for HttpError {} + +impl HttpError { + pub fn new(code: StatusCode, message: String) -> Self { + HttpError { code, message } + } +} + +impl fmt::Display for HttpError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.message) + } +} + +impl Serialize for HttpError { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut state = serializer.serialize_struct("HttpError", 2)?; + state.serialize_field("code", &self.code.as_u16())?; + state.serialize_field("message", &self.message)?; + state.end() + } +} + +/// Macro to create a HttpError inside a anyhow::Error +#[macro_export] +macro_rules! http_err { + ($status:ident, $($fmt:tt)+) => {{ + ::anyhow::Error::from($crate::HttpError::new( + $crate::StatusCode::$status, + format!($($fmt)+) + )) + }}; +} + +/// Bail with an error generated with the `http_err!` macro. +#[macro_export] +macro_rules! http_bail { + ($status:ident, $($fmt:tt)+) => {{ + return Err($crate::http_err!($status, $($fmt)+)); + }}; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_http_err() { + // Make sure the macro generates valid code. + http_err!(IM_A_TEAPOT, "Cannot brew coffee"); + } + + #[test] + fn test_http_bail() { + fn t() -> Result<(), anyhow::Error> { + // Make sure the macro generates valid code. + http_bail!( + UNAVAILABLE_FOR_LEGAL_REASONS, + "Nothing to see here, move along" + ); + } + + assert!(t().is_err()); + } +} -- 2.39.2