Skip to content

Commit

Permalink
Add 'Error::pretty_print()'.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed May 6, 2023
1 parent 07c23a4 commit be49668
Showing 1 changed file with 70 additions and 50 deletions.
120 changes: 70 additions & 50 deletions core/lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,60 +149,34 @@ impl Error {
self.mark_handled();
&self.kind
}
}

impl std::error::Error for Error { }

impl fmt::Display for ErrorKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::Bind(e) => write!(f, "binding failed: {}", e),
ErrorKind::Io(e) => write!(f, "I/O error: {}", e),
ErrorKind::Collisions(_) => "collisions detected".fmt(f),
ErrorKind::FailedFairings(_) => "launch fairing(s) failed".fmt(f),
ErrorKind::InsecureSecretKey(_) => "insecure secret key config".fmt(f),
ErrorKind::Config(_) => "failed to extract configuration".fmt(f),
ErrorKind::SentinelAborts(_) => "sentinel(s) aborted".fmt(f),
ErrorKind::Shutdown(_, Some(e)) => write!(f, "shutdown failed: {}", e),
ErrorKind::Shutdown(_, None) => "shutdown failed".fmt(f),
}
}
}

impl fmt::Debug for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.mark_handled();
self.kind().fmt(f)
}
}

impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// Prints the error with color (if enabled) and detail. Returns a string
/// that indicates the abort condition such as "aborting due to i/o error".
///
/// This function is called on `Drop` to display the error message. By
/// contrast, the `Display` implementation prints a succinct version of the
/// error, without detail.
///
/// ```rust
/// # let _ = async {
/// if let Err(error) = rocket::build().launch().await {
/// let abort = error.pretty_print();
/// panic!("{}", abort);
/// }
/// # };
/// ```
pub fn pretty_print(&self) -> &'static str {
self.mark_handled();
write!(f, "{}", self.kind())
}
}

impl Drop for Error {
fn drop(&mut self) {
// Don't panic if the message has been seen. Don't double-panic.
if self.was_handled() || std::thread::panicking() {
return
}

match self.kind() {
ErrorKind::Bind(ref e) => {
error!("Rocket failed to bind network socket to given address/port.");
info_!("{}", e);
panic!("aborting due to socket bind error");
"aborting due to socket bind error"
}
ErrorKind::Io(ref e) => {
error!("Rocket failed to launch due to an I/O error.");
info_!("{}", e);
panic!("aborting due to i/o error");
"aborting due to i/o error"
}
ErrorKind::Collisions(ref collisions) => {
fn log_collisions<T: fmt::Display>(kind: &str, collisions: &[(T, T)]) {
Expand All @@ -218,25 +192,25 @@ impl Drop for Error {
log_collisions("catcher", &collisions.catchers);

info_!("Note: Route collisions can usually be resolved by ranking routes.");
panic!("routing collisions detected");
"aborting due to detected routing collisions"
}
ErrorKind::FailedFairings(ref failures) => {
error!("Rocket failed to launch due to failing fairings:");
for fairing in failures {
info_!("{}", fairing.name);
}

panic!("aborting due to fairing failure(s)");
"aborting due to fairing failure(s)"
}
ErrorKind::InsecureSecretKey(profile) => {
error!("secrets enabled in non-debug without `secret_key`");
info_!("selected profile: {}", Paint::default(profile).bold());
info_!("disable `secrets` feature or configure a `secret_key`");
panic!("aborting due to insecure configuration")
"aborting due to insecure configuration"
}
ErrorKind::Config(error) => {
crate::config::pretty_print_error(error.clone());
panic!("aborting due to invalid configuration")
"aborting due to invalid configuration"
}
ErrorKind::SentinelAborts(ref failures) => {
error!("Rocket failed to launch due to aborting sentinels:");
Expand All @@ -246,16 +220,62 @@ impl Drop for Error {
info_!("{} ({}:{}:{})", name, file, line, col);
}

panic!("aborting due to sentinel-triggered abort(s)");
"aborting due to sentinel-triggered abort(s)"
}
ErrorKind::Shutdown(_, error) => {
error!("Rocket failed to shutdown gracefully.");
if let Some(e) = error {
info_!("{}", e);
}

panic!("aborting due to failed shutdown");
"aborting due to failed shutdown"
}
}
}
}

impl std::error::Error for Error { }

impl fmt::Display for ErrorKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::Bind(e) => write!(f, "binding failed: {}", e),
ErrorKind::Io(e) => write!(f, "I/O error: {}", e),
ErrorKind::Collisions(_) => "collisions detected".fmt(f),
ErrorKind::FailedFairings(_) => "launch fairing(s) failed".fmt(f),
ErrorKind::InsecureSecretKey(_) => "insecure secret key config".fmt(f),
ErrorKind::Config(_) => "failed to extract configuration".fmt(f),
ErrorKind::SentinelAborts(_) => "sentinel(s) aborted".fmt(f),
ErrorKind::Shutdown(_, Some(e)) => write!(f, "shutdown failed: {}", e),
ErrorKind::Shutdown(_, None) => "shutdown failed".fmt(f),
}
}
}

impl fmt::Debug for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.mark_handled();
self.kind().fmt(f)
}
}

impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.mark_handled();
write!(f, "{}", self.kind())
}
}

impl Drop for Error {
fn drop(&mut self) {
// Don't panic if the message has been seen. Don't double-panic.
if self.was_handled() || std::thread::panicking() {
return
}

panic!("{}", self.pretty_print());
}
}

0 comments on commit be49668

Please sign in to comment.