Rocket/core/lib/src/error.rs

218 lines
6.9 KiB
Rust
Raw Normal View History

//! Types representing various errors that can occur in a Rocket application.
use std::{io, fmt};
use std::sync::atomic::{Ordering, AtomicBool};
use yansi::Paint;
use http::hyper;
use router::Route;
/// The kind of launch error that occurred.
///
/// In almost every instance, a launch error occurs because of an I/O error;
/// this is represented by the `Io` variant. A launch error may also occur
/// because of ill-defined routes that lead to collisions or because a fairing
/// encountered an error; these are represented by the `Collision` and
/// `FailedFairing` variants, respectively. The `Unknown` variant captures all
/// other kinds of launch errors.
#[derive(Debug)]
pub enum LaunchErrorKind {
Bind(hyper::Error),
Io(io::Error),
Collision(Vec<(Route, Route)>),
FailedFairings(Vec<&'static str>),
Unknown(Box<::std::error::Error + Send + Sync>)
}
/// An error that occurs during launch.
///
/// 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
/// printed to the console. The following illustrates this:
///
/// ```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);
/// # }
/// ```
///
/// # 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.
pub struct LaunchError {
handled: AtomicBool,
kind: LaunchErrorKind
}
impl LaunchError {
#[inline(always)]
Overhaul URI types. This is fairly large commit with several entangled logical changes. The primary change in this commit is to completely overhaul how URI handling in Rocket works. Prior to this commit, the `Uri` type acted as an origin API. Its parser was minimal and lenient, allowing URIs that were invalid according to RFC 7230. By contrast, the new `Uri` type brings with it a strict RFC 7230 compliant parser. The `Uri` type now represents any kind of valid URI, not simply `Origin` types. Three new URI types were introduced: * `Origin` - represents valid origin URIs * `Absolute` - represents valid absolute URIs * `Authority` - represents valid authority URIs The `Origin` type replaces `Uri` in many cases: * As fields and method inputs of `Route` * The `&Uri` request guard is now `&Origin` * The `uri!` macro produces an `Origin` instead of a `Uri` The strict nature of URI parsing cascaded into the following changes: * Several `Route` methods now `panic!` on invalid URIs * The `Rocket::mount()` method is (correctly) stricter with URIs * The `Redirect` constructors take a `TryInto<Uri>` type * Dispatching of a `LocalRequest` correctly validates URIs Overall, URIs are now properly and uniformly handled throughout Rocket's codebase, resulting in a more reliable and correct system. In addition to these URI changes, the following changes are also part of this commit: * The `LocalRequest::cloned_dispatch()` method was removed in favor of chaining `.clone().dispatch()`. * The entire Rocket codebase uses `crate` instead of `pub(crate)` as a visibility modifier. * Rocket uses the `crate_visibility_modifier` and `try_from` features. A note on unsafety: this commit introduces many uses of `unsafe` in the URI parser. All of these uses are a result of unsafely transforming byte slices (`&[u8]` or similar) into strings (`&str`). The parser ensures that these casts are safe, but of course, we must label their use `unsafe`. The parser was written to be as generic and efficient as possible and thus can parse directly from byte sources. Rocket, however, does not make use of this fact and so would be able to remove all uses of `unsafe` by parsing from an existing `&str`. This should be considered in the future. Fixes #443. Resolves #263.
2018-07-29 01:26:15 +00:00
crate fn new(kind: LaunchErrorKind) -> LaunchError {
2018-07-28 16:58:10 +00:00
LaunchError { handled: AtomicBool::new(false), 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
}
}
impl From<hyper::Error> for LaunchError {
#[inline]
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)))
}
}
}
impl From<io::Error> for LaunchError {
#[inline]
fn from(error: io::Error) -> LaunchError {
LaunchError::new(LaunchErrorKind::Io(error))
}
}
impl fmt::Display for LaunchErrorKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LaunchErrorKind::Bind(ref e) => write!(f, "binding failed: {}", e),
LaunchErrorKind::Io(ref e) => write!(f, "I/O error: {}", e),
LaunchErrorKind::Collision(_) => write!(f, "route collisions detected"),
LaunchErrorKind::FailedFairings(_) => write!(f, "a launch fairing failed"),
LaunchErrorKind::Unknown(ref e) => write!(f, "unknown error: {}", e)
}
}
}
impl fmt::Debug for LaunchError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.mark_handled();
write!(f, "{:?}", self.kind())
}
}
impl fmt::Display for LaunchError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.mark_handled();
write!(f, "{}", self.kind())
}
}
impl ::std::error::Error for LaunchError {
#[inline]
fn description(&self) -> &str {
self.mark_handled();
match *self.kind() {
LaunchErrorKind::Bind(_) => "failed to bind to given address/port",
LaunchErrorKind::Io(_) => "an I/O error occurred during launch",
LaunchErrorKind::Collision(_) => "route collisions were detected",
LaunchErrorKind::FailedFairings(_) => "a launch fairing reported an error",
LaunchErrorKind::Unknown(_) => "an unknown error occurred during launch"
}
}
}
impl Drop for LaunchError {
fn drop(&mut self) {
if self.was_handled() {
return
}
match *self.kind() {
LaunchErrorKind::Bind(ref e) => {
error!("Rocket failed to bind network socket to given address/port.");
panic!("{}", e);
}
LaunchErrorKind::Io(ref e) => {
error!("Rocket failed to launch due to an I/O error.");
panic!("{}", e);
}
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.");
panic!("route collisions detected");
}
LaunchErrorKind::FailedFairings(ref failures) => {
error!("Rocket failed to launch due to failing fairings:");
for fairing in failures {
info_!("{}", Paint::white(fairing));
}
panic!("launch fairing failure");
}
LaunchErrorKind::Unknown(ref e) => {
error!("Rocket failed to launch due to an unknown error.");
panic!("{}", e);
}
}
}
}