Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to construct errors with custom description #641

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,22 @@ impl Error {
info: Some(vec![a]),
}
}

/// Create an error with a custom description.
///
/// This can be used in combination with `Error::exit` to exit your program
/// with a custom error message.
pub fn with_description(description: &str, kind: ErrorKind) -> Self {
let c = fmt::Colorizer {
use_stderr: true,
when: fmt::ColorWhen::Auto
};
Error {
message: format!("{} {}", c.error("error:"), description),
kind: kind,
info: None,
}
}
}

impl StdError for Error {
Expand All @@ -804,28 +820,12 @@ impl Display for Error {

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
let c = fmt::Colorizer {
use_stderr: true,
when: fmt::ColorWhen::Auto
};
Error {
message: format!("{} {}", c.error("error:"), e.description()),
kind: ErrorKind::Io,
info: None,
}
Error::with_description(e.description(), ErrorKind::Io)
}
}

impl From<std_fmt::Error> for Error {
fn from(e: std_fmt::Error) -> Self {
let c = fmt::Colorizer {
use_stderr: true,
when: fmt::ColorWhen::Auto
};
Error {
message: format!("{} {}", c.error("error:"), e),
kind: ErrorKind::Format,
info: None,
}
Error::with_description(e.description(), ErrorKind::Format)
}
}