feat: Improve exit codes and make them more specific
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -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
|
- View the repo maintainers as `npub` - by Awiteb
|
||||||
- New `patch list` commands to list the repo patches - by Awiteb
|
- New `patch list` commands to list the repo patches - by Awiteb
|
||||||
- New `issue list` commands to list the repo issues - by Awiteb
|
- New `issue list` commands to list the repo issues - by Awiteb
|
||||||
|
- Improve exit codes and make them more specific - by Awiteb
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ impl RelayOrSet {
|
|||||||
match self {
|
match self {
|
||||||
Self::Relay(relay) => Ok(vec![relay]),
|
Self::Relay(relay) => Ok(vec![relay]),
|
||||||
Self::Set(name) => {
|
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() {
|
if set.relays.is_empty() {
|
||||||
Err(N34Error::EmptySetRelays(name))
|
Err(N34Error::EmptySetRelays(name))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
44
src/error.rs
44
src/error.rs
@@ -21,6 +21,20 @@ use nostr_sdk::client::Error as ClientError;
|
|||||||
|
|
||||||
use crate::cli::ConfigError;
|
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<T> = Result<T, N34Error>;
|
pub type N34Result<T> = Result<T, N34Error>;
|
||||||
|
|
||||||
/// N34 errors
|
/// N34 errors
|
||||||
@@ -34,6 +48,8 @@ pub enum N34Error {
|
|||||||
EditorNotFound,
|
EditorNotFound,
|
||||||
#[error("The file you edited is empty. Please save your changes before exiting the editor.")]
|
#[error("The file you edited is empty. Please save your changes before exiting the editor.")]
|
||||||
EmptyEditorFile,
|
EmptyEditorFile,
|
||||||
|
#[error("The editor `{0}` exit with unsuccessful exit code `{1}`")]
|
||||||
|
EditorErr(String, i32),
|
||||||
#[error("Client Error: {0}")]
|
#[error("Client Error: {0}")]
|
||||||
Client(#[from] ClientError),
|
Client(#[from] ClientError),
|
||||||
#[error("Unable to locate the repository. The repository may not exists in the given relays")]
|
#[error("Unable to locate the repository. The repository may not exists in the given relays")]
|
||||||
@@ -66,6 +82,8 @@ pub enum N34Error {
|
|||||||
InvalidNostrAddressFileContent(String),
|
InvalidNostrAddressFileContent(String),
|
||||||
#[error("This command requires at least one relay, but none were provided")]
|
#[error("This command requires at least one relay, but none were provided")]
|
||||||
EmptyRelays,
|
EmptyRelays,
|
||||||
|
#[error("One naddr is required for this command")]
|
||||||
|
EmptyNaddrs,
|
||||||
#[error(
|
#[error(
|
||||||
"This command requires a signer to sign events. Use `--secret-key` to provide a signer"
|
"This command requires a signer to sign events. Use `--secret-key` to provide a signer"
|
||||||
)]
|
)]
|
||||||
@@ -77,6 +95,11 @@ pub enum N34Error {
|
|||||||
'{0}' exists."
|
'{0}' exists."
|
||||||
)]
|
)]
|
||||||
InvalidNaddrArg(String),
|
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(
|
#[error(
|
||||||
"The set '{0}' doesn't contain any addresses. Use 'sets update' to add addresses to it."
|
"The set '{0}' doesn't contain any addresses. Use 'sets update' to add addresses to it."
|
||||||
)]
|
)]
|
||||||
@@ -101,14 +124,27 @@ pub enum N34Error {
|
|||||||
RevisionRootNotFound,
|
RevisionRootNotFound,
|
||||||
#[error("Invalid status for the issue/patch: {0}")]
|
#[error("Invalid status for the issue/patch: {0}")]
|
||||||
InvalidStatus(String),
|
InvalidStatus(String),
|
||||||
#[error("One naddr is required for this command")]
|
|
||||||
EmptyNaddrs,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl N34Error {
|
impl N34Error {
|
||||||
/// Returns the exit code associated with this error
|
/// Returns the exit code associated with this error
|
||||||
pub fn exit_code(&self) -> ExitCode {
|
pub fn exit_code(&self) -> ExitCode {
|
||||||
// TODO: More specific exit code
|
match self {
|
||||||
ExitCode::FAILURE
|
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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// Disable the logs to not show up in a terminal text editor
|
||||||
crate::EDITOR_OPEN.store(true, Ordering::Relaxed);
|
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"))
|
.arg(temp_path.to_str().expect("The path is valid utf8"))
|
||||||
.spawn()?
|
.spawn()?
|
||||||
.wait()?;
|
.wait()?;
|
||||||
@@ -209,7 +209,7 @@ pub fn read_editor(file_pre_content: Option<&str>, file_suffix: &str) -> N34Resu
|
|||||||
|
|
||||||
if !exit_status.success() {
|
if !exit_status.success() {
|
||||||
if let Some(code) = exit_status.code() {
|
if let Some(code) = exit_status.code() {
|
||||||
tracing::warn!("The editor exit with `{code}` status")
|
return Err(N34Error::EditorErr(editor, code));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user