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 550E694336 for ; Wed, 21 Feb 2024 17:52:52 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 3206A1C51B for ; Wed, 21 Feb 2024 17:52:22 +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 ; Wed, 21 Feb 2024 17:52:21 +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 EA18F4458D for ; Wed, 21 Feb 2024 17:52:20 +0100 (CET) From: Max Carrara To: pbs-devel@lists.proxmox.com Date: Wed, 21 Feb 2024 17:52:10 +0100 Message-Id: <20240221165210.428072-1-m.carrara@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240220102859.71349-2-g.goller@proxmox.com> References: <20240220102859.71349-2-g.goller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.005 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 - Subject: [pbs-devel] [PATCH proxmox] fixup! CLI: print fatal errors including causes 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: Wed, 21 Feb 2024 16:52:52 -0000 --- proxmox-router/README.rst | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/proxmox-router/README.rst b/proxmox-router/README.rst index 9c7c2139..5fef633f 100644 --- a/proxmox-router/README.rst +++ b/proxmox-router/README.rst @@ -1,13 +1,13 @@ -================ - proxmox-router -================ +proxmox-router +============== -cli -================== -To improve our error handling story, we use anyhow and `.context()` on -all errors. This means no more `format_err()` and `format()` of errors. +Command Line +------------ -For example, with two calls to `.with_context` when bubbling up errors in +To improve our error handling story, we use anyhow and ``.context()`` on +all errors. This means no more ``format_err()`` and ``format()`` of errors. + +For example, with two calls to ``.with_context`` when bubbling up errors in proxmox-offline-mirror: .. code-block:: @@ -18,11 +18,11 @@ proxmox-offline-mirror: @@ -1,7 +1,7 @@ use std::fmt::Display; use std::path::Path; - + -use anyhow::{bail, Error}; +use anyhow::{bail, format_err, Context, Error}; use serde_json::Value; - + use proxmox_router::cli::{run_cli_command, CliCommand, CliCommandMap, CliEnvironment}; @@ -676,7 +676,8 @@ async fn setup(config: Option, _param: Value) -> Result<(), Error> { Action::AddMirror => { @@ -41,7 +41,7 @@ proxmox-offline-mirror: @@ -7,7 +7,7 @@ use std::{ path::{Path, PathBuf}, }; - + -use anyhow::{bail, format_err, Error}; +use anyhow::{bail, format_err, Context, Error}; use nix::{unistd, NixPath}; @@ -49,7 +49,7 @@ proxmox-offline-mirror: use proxmox_apt::deb822::CheckSums; @@ -45,10 +45,12 @@ impl Pool { } - + if !pool.exists() { - create_path(pool, None, None)?; + create_path(pool, None, None) @@ -59,7 +59,7 @@ proxmox-offline-mirror: - create_path(link_dir, None, None)?; + create_path(link_dir, None, None) + .with_context(|| format!("Failed to create link dir {link_dir:?}"))?; - + Ok(Self { pool_dir: pool.to_path_buf(), @@ -77,14 +77,14 @@ Instead of the original:: Which is not really helpful without knowing the path. -For non-fatal cases or logging inside tasks, `{:#}` could be used which just +For non-fatal cases or logging inside tasks, ``{:#}`` could be used which just prints the causes/contexts in a single line like this:: Failed to initialize mirror 'debian_bullseye_main': Failed to create pool dir "/var/lib/proxmox-offline-mirror/mirrors//.pool": EACCES: Permission denied but for that usage, the context should be kept short to avoid the line getting overly long. -One downside to adding context is that the default `Display` implementation will +One downside to adding context is that the default ``Display`` implementation will *just* print the context, which hides the root cause:: Error: Failed to initialize mirror 'debian_bullseye_main' -- 2.39.2