-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from epage/code
fix(cli): Define an error code policy
- Loading branch information
Showing
4 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,55 @@ | ||
pub struct ExitCode { | ||
pub code: sysexit::Code, | ||
pub error: Option<anyhow::Error>, | ||
} | ||
|
||
impl ExitCode { | ||
pub fn code(code: sysexit::Code) -> Self { | ||
Self { code, error: None } | ||
} | ||
|
||
pub fn error(mut self, error: anyhow::Error) -> Self { | ||
self.error = Some(error); | ||
self | ||
} | ||
} | ||
|
||
impl From<sysexit::Code> for ExitCode { | ||
fn from(code: sysexit::Code) -> Self { | ||
Self::code(code) | ||
} | ||
} | ||
|
||
pub trait ChainCodeExt { | ||
fn error(self) -> ExitCode; | ||
fn chain(self, error: anyhow::Error) -> ExitCode; | ||
} | ||
|
||
impl ChainCodeExt for sysexit::Code { | ||
fn error(self) -> ExitCode { | ||
ExitCode::code(self) | ||
} | ||
fn chain(self, error: anyhow::Error) -> ExitCode { | ||
ExitCode::code(self).error(error) | ||
} | ||
} | ||
|
||
pub trait ExitCodeResultErrorExt<T> { | ||
fn code(self, code: sysexit::Code) -> Result<T, ExitCode>; | ||
} | ||
|
||
impl<T, E: std::error::Error + Send + Sync + 'static> ExitCodeResultErrorExt<T> for Result<T, E> { | ||
fn code(self, code: sysexit::Code) -> Result<T, ExitCode> { | ||
self.map_err(|e| ExitCode::code(code).error(e.into())) | ||
} | ||
} | ||
|
||
pub trait ExitCodeResultAnyhowExt<T> { | ||
fn code(self, code: sysexit::Code) -> Result<T, ExitCode>; | ||
} | ||
|
||
impl<T> ExitCodeResultAnyhowExt<T> for Result<T, anyhow::Error> { | ||
fn code(self, code: sysexit::Code) -> Result<T, ExitCode> { | ||
self.map_err(|e| ExitCode::code(code).error(e)) | ||
} | ||
} |
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