-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |