2017-03-16 05:10:09 +00:00
|
|
|
//! Types representing various errors that can occur in a Rocket application.
|
|
|
|
|
|
|
|
use std::{io, fmt};
|
|
|
|
use std::sync::atomic::{Ordering, AtomicBool};
|
|
|
|
|
2017-08-19 01:37:25 +00:00
|
|
|
use yansi::Paint;
|
|
|
|
|
2017-03-16 05:10:09 +00:00
|
|
|
use http::hyper;
|
2017-08-19 01:37:25 +00:00
|
|
|
use router::Route;
|
2017-03-16 05:10:09 +00:00
|
|
|
|
2016-11-03 18:00:52 +00:00
|
|
|
/// [unstable] Error type for Rocket. Likely to change.
|
2016-04-04 05:41:31 +00:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
2016-03-12 18:45:19 +00:00
|
|
|
pub enum Error {
|
2016-11-03 18:00:52 +00:00
|
|
|
/// The request method was bad.
|
2016-03-15 03:43:52 +00:00
|
|
|
BadMethod,
|
2016-11-03 18:00:52 +00:00
|
|
|
/// The value could not be parsed.
|
2016-03-15 03:43:52 +00:00
|
|
|
BadParse,
|
2016-11-03 18:00:52 +00:00
|
|
|
/// There was no such route.
|
2016-10-04 21:38:47 +00:00
|
|
|
NoRoute, // TODO: Add a chain of routes attempted.
|
2016-11-03 18:00:52 +00:00
|
|
|
/// The error was internal.
|
2016-08-27 01:37:28 +00:00
|
|
|
Internal,
|
2016-11-03 18:00:52 +00:00
|
|
|
/// The requested key/index does not exist.
|
2016-09-30 22:20:11 +00:00
|
|
|
NoKey,
|
2016-03-12 18:45:19 +00:00
|
|
|
}
|
2017-03-16 05:10:09 +00:00
|
|
|
|
|
|
|
/// The kind of launch error that occured.
|
|
|
|
///
|
|
|
|
/// In almost every instance, a launch error occurs because of an I/O error;
|
2017-04-19 00:42:44 +00:00
|
|
|
/// this is represented by the `Io` variant. A launch error may also occur
|
2017-05-19 10:29:08 +00:00
|
|
|
/// because of ill-defined routes that lead to collisions or because a fairing
|
|
|
|
/// encountered an error; these are represented by the `Collision` and
|
2017-04-20 20:43:01 +00:00
|
|
|
/// `FailedFairing` variants, respectively. The `Unknown` variant captures all
|
|
|
|
/// other kinds of launch errors.
|
2017-03-16 05:10:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LaunchErrorKind {
|
|
|
|
Io(io::Error),
|
2017-08-19 01:37:25 +00:00
|
|
|
Collision(Vec<(Route, Route)>),
|
2018-02-21 11:08:54 +00:00
|
|
|
FailedFairings(Vec<&'static str>),
|
2017-03-16 05:10:09 +00:00
|
|
|
Unknown(Box<::std::error::Error + Send + Sync>)
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:08:34 +00:00
|
|
|
/// An error that occurs during launch.
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
|
|
|
/// A `LaunchError` is returned by
|
|
|
|
/// [rocket::launch](/rocket/struct.Rocket.html#method.launch) when launching an
|
|
|
|
/// application fails for some reason.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// A value of this type panics if it is dropped without first being inspected.
|
|
|
|
/// An _inspection_ occurs when any method is called. For instance, if
|
|
|
|
/// `println!("Error: {}", e)` is called, where `e: LaunchError`, the
|
|
|
|
/// `Display::fmt` method being called by `println!` results in `e` being marked
|
|
|
|
/// as inspected; a subsequent `drop` of the value will _not_ result in a panic.
|
|
|
|
/// The following snippet illustrates this:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # if false {
|
|
|
|
/// let error = rocket::ignite().launch();
|
|
|
|
///
|
|
|
|
/// // This line is only reached if launching failed. This "inspects" the error.
|
|
|
|
/// println!("Launch failed! Error: {}", error);
|
|
|
|
///
|
|
|
|
/// // This call to drop (explicit here for demonstration) will do nothing.
|
|
|
|
/// drop(error);
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// When a value of this type panics, the corresponding error message is pretty
|
2017-06-12 22:08:34 +00:00
|
|
|
/// printed to the console. The following illustrates this:
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # if false {
|
|
|
|
/// let error = rocket::ignite().launch();
|
|
|
|
///
|
|
|
|
/// // This call to drop (explicit here for demonstration) will result in
|
|
|
|
/// // `error` being pretty-printed to the console along with a `panic!`.
|
|
|
|
/// drop(error);
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2017-06-12 22:08:34 +00:00
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// A `LaunchError` value should usually be allowed to `drop` without
|
|
|
|
/// inspection. There are two exceptions to this suggestion.
|
|
|
|
///
|
|
|
|
/// 1. If you are writing a library or high-level application on-top of
|
|
|
|
/// Rocket, you likely want to inspect the value before it drops to avoid a
|
|
|
|
/// Rocket-specific `panic!`. This typically means simply printing the
|
|
|
|
/// value.
|
|
|
|
///
|
|
|
|
/// 2. You want to display your own error messages.
|
2017-03-16 05:10:09 +00:00
|
|
|
pub struct LaunchError {
|
|
|
|
handled: AtomicBool,
|
|
|
|
kind: LaunchErrorKind
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LaunchError {
|
|
|
|
#[inline(always)]
|
|
|
|
fn new(kind: LaunchErrorKind) -> LaunchError {
|
|
|
|
LaunchError { handled: AtomicBool::new(false), kind: kind }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn was_handled(&self) -> bool {
|
|
|
|
self.handled.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mark_handled(&self) {
|
|
|
|
self.handled.store(true, Ordering::Release)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the `kind` of the launch error.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # if false {
|
|
|
|
/// let error = rocket::ignite().launch();
|
|
|
|
///
|
|
|
|
/// // This line is only reached if launch failed.
|
|
|
|
/// let error_kind = error.kind();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn kind(&self) -> &LaunchErrorKind {
|
|
|
|
self.mark_handled();
|
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 00:42:44 +00:00
|
|
|
impl From<LaunchErrorKind> for LaunchError {
|
|
|
|
#[inline]
|
|
|
|
fn from(kind: LaunchErrorKind) -> LaunchError {
|
|
|
|
LaunchError::new(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 05:10:09 +00:00
|
|
|
impl From<hyper::Error> for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2017-03-16 05:10:09 +00:00
|
|
|
fn from(error: hyper::Error) -> LaunchError {
|
|
|
|
match error {
|
|
|
|
hyper::Error::Io(e) => LaunchError::new(LaunchErrorKind::Io(e)),
|
|
|
|
e => LaunchError::new(LaunchErrorKind::Unknown(Box::new(e)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 09:23:06 +00:00
|
|
|
impl From<io::Error> for LaunchError {
|
|
|
|
#[inline]
|
|
|
|
fn from(error: io::Error) -> LaunchError {
|
|
|
|
LaunchError::new(LaunchErrorKind::Io(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 05:10:09 +00:00
|
|
|
impl fmt::Display for LaunchErrorKind {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2017-03-16 05:10:09 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
LaunchErrorKind::Io(ref e) => write!(f, "I/O error: {}", e),
|
2017-08-19 01:37:25 +00:00
|
|
|
LaunchErrorKind::Collision(_) => write!(f, "route collisions detected"),
|
2018-02-21 11:08:54 +00:00
|
|
|
LaunchErrorKind::FailedFairings(_) => write!(f, "a launch fairing failed"),
|
2017-03-16 05:10:09 +00:00
|
|
|
LaunchErrorKind::Unknown(ref e) => write!(f, "unknown error: {}", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2017-03-16 05:10:09 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.mark_handled();
|
|
|
|
write!(f, "{:?}", self.kind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2017-03-16 05:10:09 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.mark_handled();
|
|
|
|
write!(f, "{}", self.kind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::error::Error for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2017-03-16 05:10:09 +00:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
self.mark_handled();
|
|
|
|
match *self.kind() {
|
|
|
|
LaunchErrorKind::Io(_) => "an I/O error occured during launch",
|
2017-08-19 01:37:25 +00:00
|
|
|
LaunchErrorKind::Collision(_) => "route collisions were detected",
|
2018-02-21 11:08:54 +00:00
|
|
|
LaunchErrorKind::FailedFairings(_) => "a launch fairing reported an error",
|
2017-03-16 05:10:09 +00:00
|
|
|
LaunchErrorKind::Unknown(_) => "an unknown error occured during launch"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LaunchError {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.was_handled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
match *self.kind() {
|
|
|
|
LaunchErrorKind::Io(ref e) => {
|
|
|
|
error!("Rocket failed to launch due to an I/O error.");
|
|
|
|
panic!("{}", e);
|
|
|
|
}
|
2017-08-19 01:37:25 +00:00
|
|
|
LaunchErrorKind::Collision(ref collisions) => {
|
|
|
|
error!("Rocket failed to launch due to the following routing collisions:");
|
|
|
|
for &(ref a, ref b) in collisions {
|
|
|
|
info_!("{} {} {}", a, Paint::red("collides with").italic(), b)
|
|
|
|
}
|
|
|
|
|
|
|
|
info_!("Note: Collisions can usually be resolved by ranking routes.");
|
2017-04-19 00:42:44 +00:00
|
|
|
panic!("route collisions detected");
|
|
|
|
}
|
2018-02-21 11:08:54 +00:00
|
|
|
LaunchErrorKind::FailedFairings(ref failures) => {
|
|
|
|
error!("Rocket failed to launch due to failing fairings:");
|
|
|
|
for fairing in failures {
|
|
|
|
info_!("{}", Paint::white(fairing));
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:43:01 +00:00
|
|
|
panic!("launch fairing failure");
|
|
|
|
}
|
2017-03-16 05:10:09 +00:00
|
|
|
LaunchErrorKind::Unknown(ref e) => {
|
|
|
|
error!("Rocket failed to launch due to an unknown error.");
|
|
|
|
panic!("{}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|