Skip to content

Commit

Permalink
Add ErrorWithExitCode struct
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Mar 2, 2023
1 parent 1eb50e8 commit c1751a1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scarb/src/bin/scarb/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use anyhow::Result;
use thiserror::Error;

pub type ScarbResult<T> = Result<T, ErrorWithExitCode>;

/// The ErrorWithExitCode is the error type used at Scarb's CLI-layer.
#[derive(Error, Debug)]
#[error("ErrorWithExitCode exit_code: {exit_code}")]
pub struct ErrorWithExitCode {
/// The error to display. This can be `None` in rare cases to exit with a
/// code without displaying a message.
#[source]
pub source: Option<anyhow::Error>,
/// The process exit code.
pub exit_code: i32,
}

impl ErrorWithExitCode {
pub fn new(error: anyhow::Error, code: i32) -> Self {
Self {
source: Some(error),
exit_code: code,
}
}

pub fn code(code: i32) -> Self {
Self {
source: None,
exit_code: code,
}
}
}

impl From<anyhow::Error> for ErrorWithExitCode {
fn from(err: anyhow::Error) -> ErrorWithExitCode {
ErrorWithExitCode::new(err, 1)
}
}

impl From<std::io::Error> for ErrorWithExitCode {
fn from(err: std::io::Error) -> ErrorWithExitCode {
ErrorWithExitCode::new(err.into(), 1)
}
}

0 comments on commit c1751a1

Please sign in to comment.