pub struct ExitError {
pub message: String,
pub exit_code: ExitCode,
pub context: Option<String>,
}
Expand description
Representation of errors that ends a process/program.
Error implements From<ExitError>
, use ?
and .into()
in functions and closures to convert to the Error::Domain variant.
Note: ExitError convert to Error by converting first to DomainError.
§Examples
Domain errors that derive ExitError must implement From<#MyDomainError> for ExitError
.
use explicit_error_exit::{prelude::*, ExitError, Result, derive::ExitError};
use std::process::ExitCode;
#[derive(ExitError, Debug)]
enum MyError {
Foo,
}
impl From<&MyError> for ExitError {
fn from(value: &MyError) -> Self {
match value {
MyError::Foo => ExitError::new(
"Something went wrong because ..",
ExitCode::from(42)
),
}
}
}
Domain errors cannot require to be extracted in either a struct or enum variant. You can generate Error::Domain variant with an ExitError
use explicit_error_exit::{prelude::*, ExitError, Result, Fault};
use std::process::ExitCode;
fn business_logic() -> Result<()> {
Err(42).map_err(|e|
ExitError::new(
"Something went wrong because ..",
ExitCode::from(e)
)
)?;
Ok(())
}
Fields§
§message: String
§exit_code: ExitCode
§context: Option<String>
Implementations§
Source§impl ExitError
impl ExitError
Sourcepub fn new(message: impl Display, exit_code: ExitCode) -> Self
pub fn new(message: impl Display, exit_code: ExitCode) -> Self
Generate an ExitError without a context. To add a context use with_context afterwards.
§Examples
use explicit_error_exit::ExitError;
use std::process::ExitCode;
ExitError::new(
"Something went wrong because ..",
ExitCode::from(42)
);
Sourcepub fn with_context(self, context: impl Display) -> Self
pub fn with_context(self, context: impl Display) -> Self
Add a context to an ExitError, override if one was set. The context appears in display but not in the Display implementation.
§Examples
use explicit_error_exit::ExitError;
use std::process::ExitCode;
ExitError::new(
"Something went wrong because ..",
ExitCode::from(42)
).with_context("The reason why it went wrong");
Trait Implementations§
Source§impl Error for ExitError
impl Error for ExitError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for ExitError
impl RefUnwindSafe for ExitError
impl Send for ExitError
impl Sync for ExitError
impl Unpin for ExitError
impl UnwindSafe for ExitError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more