pub struct ErrorMessage { /* private fields */ }
Expand description
To get an ErrorMessage without an underlying Error.
use errors_with_context::ErrorMessage;
ErrorMessage::new("Error description".to_owned());
// prints "Error description" without listing a cause
Most of the time, you need a Result<T, ErrorMessage> instead.
ErrorMessage::err does exactly that, so you can immediately throw it with ?
:
fn erroring_function() -> Result<String, ErrorMessage> {
ErrorMessage::err("Error description".to_owned())?
// [...]
}
If you want to manually wrap an Error, there is the function ErrorMessage::with_context.
ErrorMessage::with_context("Error description".to_owned(), io::Error::last_os_error());
Implementations§
Source§impl ErrorMessage
impl ErrorMessage
Sourcepub fn new(message: String) -> ErrorMessage
pub fn new(message: String) -> ErrorMessage
To get an ErrorMessage without an underlying Error as a cause.
Example:
use errors_with_context::ErrorMessage;
ErrorMessage::new("Error description".to_owned());
// prints "Error description" without listing a cause
Sourcepub fn err<T>(message: String) -> Result<T, ErrorMessage>
pub fn err<T>(message: String) -> Result<T, ErrorMessage>
This function creates a Result<T, ErrorMessage>, so you can immediately throw it with ?
.
Example:
fn erroring_function() -> Result<String, ErrorMessage> {
ErrorMessage::err("Error description".to_owned())?
// [...]
}
Sourcepub fn with_context<E: Error + 'static>(
message: String,
cause: E,
) -> ErrorMessage
pub fn with_context<E: Error + 'static>( message: String, cause: E, ) -> ErrorMessage
This function allows one to manually wrap an Error.
Example:
ErrorMessage::with_context("Error description".to_owned(), io::Error::last_os_error());
Trait Implementations§
Source§impl Debug for ErrorMessage
impl Debug for ErrorMessage
Source§impl Display for ErrorMessage
impl Display for ErrorMessage
Source§impl Error for ErrorMessage
impl Error for ErrorMessage
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 ErrorMessage
impl !RefUnwindSafe for ErrorMessage
impl !Send for ErrorMessage
impl !Sync for ErrorMessage
impl Unpin for ErrorMessage
impl !UnwindSafe for ErrorMessage
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