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
|
|
|
|
2017-06-12 22:08:34 +00:00
|
|
|
/// An error that occurs during launch.
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2020-09-03 05:41:31 +00:00
|
|
|
/// An `Error` is returned by [`launch()`](crate::Rocket::launch()) when
|
|
|
|
/// launching an application fails or, more rarely, when the runtime fails after
|
|
|
|
/// lauching.
|
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
|
2020-09-03 05:41:31 +00:00
|
|
|
/// `println!("Error: {}", e)` is called, where `e: Error`, 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:
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2019-09-08 20:53:53 +00:00
|
|
|
/// ```rust
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # let _ = async {
|
|
|
|
/// if let Err(error) = rocket::ignite().launch().await {
|
2020-09-03 05:41:31 +00:00
|
|
|
/// // This println "inspects" the error.
|
|
|
|
/// println!("Launch failed! Error: {}", error);
|
2017-03-16 05:10:09 +00:00
|
|
|
///
|
2020-09-03 05:41:31 +00:00
|
|
|
/// // This call to drop (explicit here for demonstration) will do nothing.
|
|
|
|
/// drop(error);
|
2019-09-08 20:53:53 +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
|
|
|
|
///
|
2020-09-03 05:41:31 +00:00
|
|
|
/// An `Error` value should usually be allowed to `drop` without inspection.
|
|
|
|
/// There are at least two exceptions:
|
2017-06-12 22:08:34 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2020-09-03 05:41:31 +00:00
|
|
|
pub struct Error {
|
2017-03-16 05:10:09 +00:00
|
|
|
handled: AtomicBool,
|
2020-09-03 05:41:31 +00:00
|
|
|
kind: ErrorKind
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The kind 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.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ErrorKind {
|
|
|
|
/// Binding to the provided address/port failed.
|
|
|
|
Bind(io::Error),
|
|
|
|
/// An I/O error occurred during launch.
|
|
|
|
Io(io::Error),
|
|
|
|
/// An I/O error occurred in the runtime.
|
|
|
|
Runtime(Box<dyn std::error::Error + Send + Sync>),
|
|
|
|
/// Route collisions were detected.
|
|
|
|
Collision(Vec<(Route, Route)>),
|
|
|
|
/// A launch fairing reported an error.
|
|
|
|
FailedFairings(Vec<&'static str>),
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl From<ErrorKind> for Error {
|
|
|
|
fn from(kind: ErrorKind) -> Self {
|
|
|
|
Error::new(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
2017-03-16 05:10:09 +00:00
|
|
|
#[inline(always)]
|
2020-09-03 05:41:31 +00:00
|
|
|
pub(crate) fn new(kind: ErrorKind) -> Error {
|
|
|
|
Error { 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
|
2020-09-03 05:41:31 +00:00
|
|
|
/// use rocket::error::ErrorKind;
|
|
|
|
///
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # let _ = async {
|
|
|
|
/// if let Err(error) = rocket::ignite().launch().await {
|
2020-09-03 05:41:31 +00:00
|
|
|
/// match error.kind() {
|
|
|
|
/// ErrorKind::Io(e) => println!("found an i/o launch error: {}", e),
|
|
|
|
/// e => println!("something else happened: {}", e)
|
2019-08-25 02:19:11 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
2020-06-14 15:57:55 +00:00
|
|
|
/// # };
|
2017-03-16 05:10:09 +00:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2020-09-03 05:41:31 +00:00
|
|
|
pub fn kind(&self) -> &ErrorKind {
|
2017-03-16 05:10:09 +00:00
|
|
|
self.mark_handled();
|
|
|
|
&self.kind
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 23:04:25 +00:00
|
|
|
impl std::error::Error for Error { }
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl fmt::Display for ErrorKind {
|
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 {
|
2020-09-03 05:41:31 +00:00
|
|
|
match self {
|
|
|
|
ErrorKind::Bind(e) => write!(f, "binding failed: {}", e),
|
|
|
|
ErrorKind::Io(e) => write!(f, "I/O error: {}", e),
|
|
|
|
ErrorKind::Collision(_) => write!(f, "route collisions detected"),
|
|
|
|
ErrorKind::FailedFairings(_) => write!(f, "a launch fairing failed"),
|
|
|
|
ErrorKind::Runtime(e) => write!(f, "runtime error: {}", e)
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl fmt::Debug for Error {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl fmt::Display for Error {
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl Drop for Error {
|
2017-03-16 05:10:09 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.was_handled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
match *self.kind() {
|
2020-09-03 05:41:31 +00:00
|
|
|
ErrorKind::Bind(ref e) => {
|
2018-04-09 00:38:25 +00:00
|
|
|
error!("Rocket failed to bind network socket to given address/port.");
|
2020-09-03 05:41:31 +00:00
|
|
|
info_!("{}", e);
|
UTF-8 routes. Forms revamp. Temp files. Capped.
So. Many. Changes.
This is an insane commit: simultaneously one of the best (because of all
the wonderful improvements!) and one of the worst (because it is just
massive) in the project's history.
Routing:
* All UTF-8 characters are accepted everywhere in route paths. (#998)
* `path` is now `uri` in `route` attribute: `#[route(GET, path = "..")]`
becomes `#[route(GET, uri = "..")]`.
Forms Revamp
* All form related types now reside in a new `form` module.
* Multipart forms are supported. (resolves #106)
* Collections are supported in forms and queries. (resolves #205)
* Nested structures in forms and queries are supported. (resolves #313)
* Form fields can be ad-hoc validated with `#[field(validate = expr)]`.
* `FromFormValue` is now `FromFormField`, blanket implements `FromForm`.
* Form field values are always percent-decoded apriori.
Temporary Files
* A new `TempFile` data and form guard allows streaming data directly to a
file which can then be persisted.
* A new `temp_dir` config parameter specifies where to store `TempFile`.
* The limits `file` and `file/$ext`, where `$ext` is the file extension,
determines the data limit for a `TempFile`.
Capped
* A new `Capped` type is used to indicate when data has been truncated due to
incoming data limits. It allows checking whether data is complete or
truncated.
* `DataStream` methods return `Capped` types.
* `DataStream` API has been revamped to account for `Capped` types.
* Several `Capped<T>` types implement `FromData`, `FromForm`.
* HTTP 413 (Payload Too Large) errors are now returned when data limits are
exceeded. (resolves #972)
Hierarchical Limits
* Data limits are now hierarchical, delimited with `/`. A limit of `a/b/c`
falls back to `a/b` then `a`.
Core
* `&RawStr` no longer implements `FromParam`.
* `&str` implements `FromParam`, `FromData`, `FromForm`.
* `FromTransformedData` was removed.
* `FromData` gained a lifetime for use with request-local data.
* The default error HTML is more compact.
* `&Config` is a request guard.
* The `DataStream` interface was entirely revamped.
* `State` is only exported via `rocket::State`.
* A `request::local_cache!()` macro was added for storing values in
request-local cache without consideration for type uniqueness by using a
locally generated anonymous type.
* `Request::get_param()` is now `Request::param()`.
* `Request::get_segments()` is now `Request::segments()`, takes a range.
* `Request::get_query_value()` is now `Request::query_value()`, can parse any
`FromForm` including sequences.
* `std::io::Error` implements `Responder` like `Debug<std::io::Error>`.
* `(Status, R)` where `R: Responder` implements `Responder` by overriding the
`Status` of `R`.
* The name of a route is printed first during route matching.
* `FlashMessage` now only has one lifetime generic.
HTTP
* `RawStr` implements `serde::{Serialize, Deserialize}`.
* `RawStr` implements _many_ more methods, in particular, those related to the
`Pattern` API.
* `RawStr::from_str()` is now `RawStr::new()`.
* `RawStr::url_decode()` and `RawStr::url_decode_lossy()` only allocate as
necessary, return `Cow`.
* `Status` implements `Default` with `Status::Ok`.
* `Status` implements `PartialEq`, `Eq`, `Hash`, `PartialOrd`, `Ord`.
* Authority and origin part of `Absolute` can be modified with new
`Absolute::{with,set}_authority()`, `Absolute::{with,set}_origin()` methods.
* `Origin::segments()` was removed in favor of methods split into query and
path parts and into raw and decoded versions.
* The `Segments` iterator is smarter, returns decoded `&str` items.
* `Segments::into_path_buf()` is now `Segments::to_path_buf()`.
* A new `QuerySegments` is the analogous query segment iterator.
* Once set, `expires` on private cookies is not overwritten. (resolves #1506)
* `Origin::path()` and `Origin::query()` return `&RawStr`, not `&str`.
Codegen
* Preserve more spans in `uri!` macro.
* Preserve spans `FromForm` field types.
* All dynamic parameters in a query string must typecheck as `FromForm`.
* `FromFormValue` derive removed; `FromFormField` added.
* The `form` `FromForm` and `FromFormField` field attribute is now named
`field`. `#[form(field = ..)]` is now `#[field(name = ..)]`.
Contrib
* `Json` implements `FromForm`.
* `MsgPack` implements `FromForm`.
* The `json!` macro is exported as `rocket_contrib::json::json!`.
* Added clarifying docs to `StaticFiles`.
Examples
* `form_validation` and `form_kitchen_sink` removed in favor of `forms`.
* The `hello_world` example uses unicode in paths.
* The `json` example only allocates as necessary.
Internal
* Codegen uses new `exports` module with the following conventions:
- Locals starts with `__` and are lowercased.
- Rocket modules start with `_` and are lowercased.
- `std` types start with `_` and are titlecased.
- Rocket types are titlecased.
* A `header` module was added to `http`, contains header types.
* `SAFETY` is used as doc-string keyword for `unsafe` related comments.
* The `Uri` parser no longer recognizes Rocket route URIs.
2020-10-30 03:50:06 +00:00
|
|
|
panic!("aborting due to socket bind error");
|
2018-02-01 20:26:02 +00:00
|
|
|
}
|
2020-09-03 05:41:31 +00:00
|
|
|
ErrorKind::Io(ref e) => {
|
2017-03-16 05:10:09 +00:00
|
|
|
error!("Rocket failed to launch due to an I/O error.");
|
2020-09-03 05:41:31 +00:00
|
|
|
info_!("{}", e);
|
|
|
|
panic!("aborting due to i/o error");
|
2017-03-16 05:10:09 +00:00
|
|
|
}
|
2020-09-03 05:41:31 +00:00
|
|
|
ErrorKind::Collision(ref collisions) => {
|
2017-08-19 01:37:25 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-09-03 05:41:31 +00:00
|
|
|
ErrorKind::FailedFairings(ref failures) => {
|
2018-02-21 11:08:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
panic!("aborting due to launch fairing failure");
|
|
|
|
}
|
|
|
|
ErrorKind::Runtime(ref err) => {
|
|
|
|
error!("An error occured in the runtime:");
|
|
|
|
info_!("{}", err);
|
|
|
|
panic!("aborting due to runtime failure");
|
2017-04-20 20:43:01 +00:00
|
|
|
}
|
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;
|
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 route URI is not a valid URI.
|
|
|
|
Uri(uri::Error<'static>),
|
|
|
|
/// The base (mount point) contains dynamic segments.
|
|
|
|
DynamicBase,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-03-04 10:49:29 +00:00
|
|
|
RouteUriError::DynamicBase => write!(f, "Mount point contains dynamic parameters."),
|
|
|
|
RouteUriError::Uri(error) => write!(f, "Malformed URI: {}", error)
|
2018-10-10 11:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|