use std::{io, fmt};
use std::sync::atomic::{Ordering, AtomicBool};
use yansi::Paint;
use http::hyper;
use router::Route;
#[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>)
}
pub struct LaunchError {
handled: AtomicBool,
kind: LaunchErrorKind
}
impl LaunchError {
#[inline(always)]
crate fn new(kind: LaunchErrorKind) -> LaunchError {
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)
}
#[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_!("{}", fairing);
}
panic!("launch fairing failure");
}
LaunchErrorKind::Unknown(ref e) => {
error!("Rocket failed to launch due to an unknown error.");
panic!("{}", e);
}
}
}
}
use http::uri;
use http::ext::IntoOwned;
use http::route::{Error as SegmentError};
#[derive(Debug)]
pub enum RouteUriError {
Segment,
Uri(uri::Error<'static>),
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 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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)
}
}
}
}