feat: Improve exit codes and make them more specific

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb
2025-07-04 10:49:45 +00:00
parent 05b4ae30f7
commit 3510b59fbb
4 changed files with 46 additions and 7 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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
/// users 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<T> = Result<T, N34Error>;
/// 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,
}
}
}

View File

@@ -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));
}
}