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;
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::router::Route;
|
2017-03-16 05:10:09 +00:00
|
|
|
|
2020-01-16 00:13:12 +00:00
|
|
|
/// An error that occurs when running a Rocket server.
|
|
|
|
///
|
|
|
|
/// Errors can happen immediately upon launch ([`LaunchError`])
|
|
|
|
/// or more rarely during the server's execution.
|
2019-08-25 02:19:11 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Launch(LaunchError),
|
2020-01-16 00:13:12 +00:00
|
|
|
Run(Box<dyn std::error::Error + Send + Sync>),
|
2019-08-25 02:19:11 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 00:13:12 +00:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Error::Launch(e) => write!(f, "Rocket failed to launch: {}", e),
|
|
|
|
Error::Run(e) => write!(f, "error while running server: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error { }
|
|
|
|
|
2018-07-12 03:44:09 +00:00
|
|
|
/// The kind of launch error that occurred.
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2020-01-16 00:13:12 +00:00
|
|
|
/// `FailedFairing` variants, respectively.
|
2017-03-16 05:10:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum LaunchErrorKind {
|
2018-10-10 11:20:25 +00:00
|
|
|
/// Binding to the provided address/port failed.
|
2019-09-14 15:10:00 +00:00
|
|
|
Bind(io::Error),
|
2018-10-10 11:20:25 +00:00
|
|
|
/// An I/O error occurred during launch.
|
2017-03-16 05:10:09 +00:00
|
|
|
Io(io::Error),
|
2018-10-10 11:20:25 +00:00
|
|
|
/// Route collisions were detected.
|
2017-08-19 01:37:25 +00:00
|
|
|
Collision(Vec<(Route, Route)>),
|
2018-10-10 11:20:25 +00:00
|
|
|
/// A launch fairing reported an error.
|
2018-02-21 11:08:54 +00:00
|
|
|
FailedFairings(Vec<&'static str>),
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 22:08:34 +00:00
|
|
|
/// An error that occurs during launch.
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2019-06-13 01:48:02 +00:00
|
|
|
/// A `LaunchError` is returned by [`launch()`](crate::Rocket::launch()) when
|
2018-10-06 13:25:17 +00:00
|
|
|
/// launching an application fails.
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
|
|
|
/// # 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:
|
|
|
|
///
|
2019-09-08 20:53:53 +00:00
|
|
|
/// ```rust
|
|
|
|
/// use rocket::error::Error;
|
|
|
|
///
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # let _ = async {
|
|
|
|
/// if let Err(error) = rocket::ignite().launch().await {
|
2019-09-08 20:53:53 +00:00
|
|
|
/// match error {
|
|
|
|
/// Error::Launch(error) => {
|
|
|
|
/// // This case is only reached if launching failed. This println "inspects" the error.
|
|
|
|
/// println!("Launch failed! Error: {}", error);
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2019-09-08 20:53:53 +00:00
|
|
|
/// // This call to drop (explicit here for demonstration) will do nothing.
|
|
|
|
/// drop(error);
|
|
|
|
/// }
|
|
|
|
/// Error::Run(error) => {
|
|
|
|
/// // This case is reached if launching succeeds, but the server had a fatal error later
|
|
|
|
/// println!("Server failed! Error: {}", error);
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # };
|
2017-03-16 05:10:09 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// 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
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # let _ = async {
|
|
|
|
/// let error = rocket::ignite().launch().await;
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
|
|
|
/// // This call to drop (explicit here for demonstration) will result in
|
|
|
|
/// // `error` being pretty-printed to the console along with a `panic!`.
|
|
|
|
/// drop(error);
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # };
|
2017-03-16 05:10:09 +00:00
|
|
|
/// ```
|
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)]
|
2019-09-20 20:43:05 +00:00
|
|
|
pub(crate) fn new(kind: LaunchErrorKind) -> LaunchError {
|
2018-07-28 16:58:10 +00:00
|
|
|
LaunchError { handled: AtomicBool::new(false), kind }
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
2019-08-25 02:19:11 +00:00
|
|
|
/// use rocket::error::Error;
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # let _ = async {
|
|
|
|
/// if let Err(error) = rocket::ignite().launch().await {
|
2019-08-25 02:19:11 +00:00
|
|
|
/// match error {
|
|
|
|
/// Error::Launch(err) => println!("Found a launch error: {}", err.kind()),
|
|
|
|
/// Error::Run(err) => println!("Error at runtime"),
|
|
|
|
/// }
|
|
|
|
/// }
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # };
|
2017-03-16 05:10:09 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn kind(&self) -> &LaunchErrorKind {
|
|
|
|
self.mark_handled();
|
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-03-16 05:10:09 +00:00
|
|
|
match *self {
|
2018-04-09 00:38:25 +00:00
|
|
|
LaunchErrorKind::Bind(ref e) => write!(f, "binding failed: {}", e),
|
2017-03-16 05:10:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-03-16 05:10:09 +00:00
|
|
|
self.mark_handled();
|
2019-09-14 04:07:19 +00:00
|
|
|
self.kind().fmt(f)
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for LaunchError {
|
2017-04-13 07:18:31 +00:00
|
|
|
#[inline]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2017-03-16 05:10:09 +00:00
|
|
|
self.mark_handled();
|
|
|
|
write!(f, "{}", self.kind())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for LaunchError {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.was_handled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
match *self.kind() {
|
2018-02-01 20:26:02 +00:00
|
|
|
LaunchErrorKind::Bind(ref e) => {
|
2018-04-09 00:38:25 +00:00
|
|
|
error!("Rocket failed to bind network socket to given address/port.");
|
2018-02-01 20:26:02 +00:00
|
|
|
panic!("{}", e);
|
|
|
|
}
|
2017-03-16 05:10:09 +00:00
|
|
|
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 {
|
2018-11-19 10:11:38 +00:00
|
|
|
info_!("{}", fairing);
|
2018-02-21 11:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 20:43:01 +00:00
|
|
|
panic!("launch fairing failure");
|
|
|
|
}
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 11:20:25 +00:00
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::http::uri;
|
|
|
|
use crate::http::ext::IntoOwned;
|
2020-07-22 19:21:19 +00:00
|
|
|
use crate::http::route::Error as SegmentError;
|
2018-10-10 11:20:25 +00:00
|
|
|
|
2020-07-22 19:21:19 +00:00
|
|
|
/// Error returned by [`Route::map_base()`] on invalid URIs.
|
2018-10-10 11:20:25 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum RouteUriError {
|
|
|
|
/// The base (mount point) or route path contains invalid segments.
|
|
|
|
Segment,
|
|
|
|
/// The route URI is not a valid URI.
|
|
|
|
Uri(uri::Error<'static>),
|
|
|
|
/// The base (mount point) contains dynamic segments.
|
|
|
|
DynamicBase,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<(&'a str, SegmentError<'a>)> for RouteUriError {
|
|
|
|
fn from(_: (&'a str, SegmentError<'a>)) -> Self {
|
|
|
|
RouteUriError::Segment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<uri::Error<'a>> for RouteUriError {
|
|
|
|
fn from(error: uri::Error<'a>) -> Self {
|
|
|
|
RouteUriError::Uri(error.into_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for RouteUriError {
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-10-10 11:20:25 +00:00
|
|
|
match self {
|
|
|
|
RouteUriError::Segment => {
|
|
|
|
write!(f, "The URI contains malformed dynamic route path segments.")
|
|
|
|
}
|
|
|
|
RouteUriError::DynamicBase => {
|
|
|
|
write!(f, "The mount point contains dynamic parameters.")
|
|
|
|
}
|
|
|
|
RouteUriError::Uri(error) => {
|
|
|
|
write!(f, "Malformed URI: {}", error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|