From 3510b59fbb439ffc59fec88fdca0236d074927d2 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Fri, 4 Jul 2025 10:49:45 +0000
Subject: [PATCH] feat: Improve exit codes and make them more specific
Signed-off-by: Awiteb
---
CHANGELOG.md | 1 +
src/cli/types.rs | 4 +++-
src/error.rs | 44 ++++++++++++++++++++++++++++++++++++----
src/nostr_utils/utils.rs | 4 ++--
4 files changed, 46 insertions(+), 7 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a2049c5..9dc0467 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- View the repo maintainers as `npub` - by Awiteb
- New `patch list` commands to list the repo patches - by Awiteb
- New `issue list` commands to list the repo issues - by Awiteb
+- Improve exit codes and make them more specific - by Awiteb
### Dependencies
diff --git a/src/cli/types.rs b/src/cli/types.rs
index 4d1810f..7e40649 100644
--- a/src/cli/types.rs
+++ b/src/cli/types.rs
@@ -89,7 +89,9 @@ impl RelayOrSet {
match self {
Self::Relay(relay) => Ok(vec![relay]),
Self::Set(name) => {
- let set = sets.get_set(&name)?;
+ let set = sets
+ .get_set(&name)
+ .map_err(|_| N34Error::InvalidRelaysArg(name.clone()))?;
if set.relays.is_empty() {
Err(N34Error::EmptySetRelays(name))
} else {
diff --git a/src/error.rs b/src/error.rs
index d2666e0..297faf3 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -21,6 +21,20 @@ use nostr_sdk::client::Error as ClientError;
use crate::cli::ConfigError;
+/// The input data was incorrect in some way. This should only be used for
+/// user’s data and not system file.
+const DATA_ERROR: u8 = 65;
+
+/// An internal software error has been detected. This should be limited to
+/// non-operating system related errors.
+const SOFTWARE_ERROR: u8 = 70;
+
+/// An error occurred while doing I/O on some file.
+const IO_ERROR: u8 = 74;
+
+/// Something was found in an unconfigured or misconfigured state.
+const CONFIG_ERROR: u8 = 78;
+
pub type N34Result = Result;
/// N34 errors
@@ -34,6 +48,8 @@ pub enum N34Error {
EditorNotFound,
#[error("The file you edited is empty. Please save your changes before exiting the editor.")]
EmptyEditorFile,
+ #[error("The editor `{0}` exit with unsuccessful exit code `{1}`")]
+ EditorErr(String, i32),
#[error("Client Error: {0}")]
Client(#[from] ClientError),
#[error("Unable to locate the repository. The repository may not exists in the given relays")]
@@ -66,6 +82,8 @@ pub enum N34Error {
InvalidNostrAddressFileContent(String),
#[error("This command requires at least one relay, but none were provided")]
EmptyRelays,
+ #[error("One naddr is required for this command")]
+ EmptyNaddrs,
#[error(
"This command requires a signer to sign events. Use `--secret-key` to provide a signer"
)]
@@ -77,6 +95,11 @@ pub enum N34Error {
'{0}' exists."
)]
InvalidNaddrArg(String),
+ #[error(
+ "Invalid relays. Expected a relay url or a set name that contains some relays\nError: No \
+ set named '{0}' exists."
+ )]
+ InvalidRelaysArg(String),
#[error(
"The set '{0}' doesn't contain any addresses. Use 'sets update' to add addresses to it."
)]
@@ -101,14 +124,27 @@ pub enum N34Error {
RevisionRootNotFound,
#[error("Invalid status for the issue/patch: {0}")]
InvalidStatus(String),
- #[error("One naddr is required for this command")]
- EmptyNaddrs,
}
impl N34Error {
/// Returns the exit code associated with this error
pub fn exit_code(&self) -> ExitCode {
- // TODO: More specific exit code
- ExitCode::FAILURE
+ match self {
+ Self::Io(_) | Self::CanNotReadNostrAddressFile(_) => ExitCode::from(IO_ERROR),
+ Self::Config(_) => ExitCode::from(CONFIG_ERROR),
+ Self::EditorErr(..) => ExitCode::from(SOFTWARE_ERROR),
+ Self::InvalidRepoId
+ | Self::EmptyNostrAddressFile
+ | Self::InvalidNostrAddressFileContent(_)
+ | Self::EmptyRelays
+ | Self::EmptyNaddrs
+ | Self::SignerRequired
+ | Self::InvalidNaddrArg(_)
+ | Self::InvalidRelaysArg(_)
+ | Self::EmptySetNaddrs(_)
+ | Self::EmptySetRelays(_)
+ | Self::NotRootPatch => ExitCode::from(DATA_ERROR),
+ _ => ExitCode::FAILURE,
+ }
}
}
diff --git a/src/nostr_utils/utils.rs b/src/nostr_utils/utils.rs
index f4f490f..3b95ef3 100644
--- a/src/nostr_utils/utils.rs
+++ b/src/nostr_utils/utils.rs
@@ -201,7 +201,7 @@ pub fn read_editor(file_pre_content: Option<&str>, file_suffix: &str) -> N34Resu
// Disable the logs to not show up in a terminal text editor
crate::EDITOR_OPEN.store(true, Ordering::Relaxed);
- let exit_status = std::process::Command::new(editor)
+ let exit_status = std::process::Command::new(&editor)
.arg(temp_path.to_str().expect("The path is valid utf8"))
.spawn()?
.wait()?;
@@ -209,7 +209,7 @@ pub fn read_editor(file_pre_content: Option<&str>, file_suffix: &str) -> N34Resu
if !exit_status.success() {
if let Some(code) = exit_status.code() {
- tracing::warn!("The editor exit with `{code}` status")
+ return Err(N34Error::EditorErr(editor, code));
}
}