mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-29 12:52:05 +00:00
Remove neglected and obscure 'Error' type.
This commit is contained in:
parent
351757c6ee
commit
46afabdfea
@ -227,7 +227,8 @@ impl Handler for StaticFiles {
|
||||
// Otherwise, we're handling segments. Get the segments as a `PathBuf`,
|
||||
// only allowing dotfiles if the user allowed it.
|
||||
let allow_dotfiles = self.options.contains(Options::DotFiles);
|
||||
let path = req.get_segments::<Segments>(0).ok()
|
||||
let path = req.get_segments::<Segments>(0)
|
||||
.and_then(|res| res.ok())
|
||||
.and_then(|segments| segments.into_path_buf(allow_dotfiles).ok())
|
||||
.map(|path| self.root.join(path))
|
||||
.into_outcome(Status::NotFound)?;
|
||||
|
@ -83,7 +83,6 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result<TokenStream> {
|
||||
#user_catcher_fn
|
||||
|
||||
#vis fn #generated_fn_name<'_b>(
|
||||
_: ::rocket::Error,
|
||||
__req: &'_b ::rocket::Request
|
||||
) -> ::rocket::response::Result<'_b> {
|
||||
let response = #catcher_response;
|
||||
|
@ -1,7 +1,6 @@
|
||||
use response;
|
||||
use handler::ErrorHandler;
|
||||
use codegen::StaticCatchInfo;
|
||||
use error::Error;
|
||||
use request::Request;
|
||||
|
||||
use std::fmt;
|
||||
@ -76,17 +75,17 @@ impl Catcher {
|
||||
///
|
||||
/// ```rust
|
||||
/// # #![allow(unused_variables)]
|
||||
/// use rocket::{Catcher, Request, Error};
|
||||
/// use rocket::{Catcher, Request};
|
||||
/// use rocket::response::{Result, Responder};
|
||||
/// use rocket::response::status::Custom;
|
||||
/// use rocket::http::Status;
|
||||
///
|
||||
/// fn handle_404<'r>(_: Error, req: &'r Request) -> Result<'r> {
|
||||
/// fn handle_404<'r>(req: &'r Request) -> Result<'r> {
|
||||
/// let res = Custom(Status::NotFound, format!("404: {}", req.uri()));
|
||||
/// res.respond_to(req)
|
||||
/// }
|
||||
///
|
||||
/// fn handle_500<'r>(_: Error, req: &'r Request) -> Result<'r> {
|
||||
/// fn handle_500<'r>(req: &'r Request) -> Result<'r> {
|
||||
/// "Whoops, we messed up!".respond_to(req)
|
||||
/// }
|
||||
///
|
||||
@ -99,8 +98,8 @@ impl Catcher {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
crate fn handle<'r>(&self, e: Error, r: &'r Request) -> response::Result<'r> {
|
||||
(self.handler)(e, r)
|
||||
crate fn handle<'r>(&self, req: &'r Request) -> response::Result<'r> {
|
||||
(self.handler)(req)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@ -150,7 +149,7 @@ macro_rules! default_catchers {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
$(
|
||||
fn $fn_name<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
||||
fn $fn_name<'r>(req: &'r Request) -> response::Result<'r> {
|
||||
status::Custom(Status::from_code($code).unwrap(),
|
||||
content::Html(error_page_template!($code, $name, $description))
|
||||
).respond_to(req)
|
||||
@ -171,7 +170,6 @@ pub mod defaults {
|
||||
use request::Request;
|
||||
use response::{self, content, status, Responder};
|
||||
use http::Status;
|
||||
use error::Error;
|
||||
|
||||
pub fn get() -> HashMap<u16, Catcher> {
|
||||
default_catchers! {
|
||||
|
@ -8,21 +8,6 @@ use yansi::Paint;
|
||||
use http::hyper;
|
||||
use router::Route;
|
||||
|
||||
/// [unstable] Error type for Rocket. Likely to change.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum Error {
|
||||
/// The request method was bad.
|
||||
BadMethod,
|
||||
/// The value could not be parsed.
|
||||
BadParse,
|
||||
/// There was no such route.
|
||||
NoRoute, // TODO: Add a chain of routes attempted.
|
||||
/// The error was internal.
|
||||
Internal,
|
||||
/// The requested key/index does not exist.
|
||||
NoKey,
|
||||
}
|
||||
|
||||
/// The kind of launch error that occurred.
|
||||
///
|
||||
/// In almost every instance, a launch error occurs because of an I/O error;
|
||||
|
@ -3,7 +3,6 @@
|
||||
use data::Data;
|
||||
use request::Request;
|
||||
use response::{self, Response, Responder};
|
||||
use error::Error;
|
||||
use http::Status;
|
||||
use outcome;
|
||||
|
||||
@ -180,7 +179,7 @@ impl<F: Clone + Sync + Send + 'static> Handler for F
|
||||
}
|
||||
|
||||
/// The type of an error handler.
|
||||
pub type ErrorHandler = for<'r> fn(Error, &'r Request) -> response::Result<'r>;
|
||||
pub type ErrorHandler = for<'r> fn(&'r Request) -> response::Result<'r>;
|
||||
|
||||
impl<'r> Outcome<'r> {
|
||||
/// Return the `Outcome` of response to `req` from `responder`.
|
||||
|
@ -150,7 +150,6 @@ mod ext;
|
||||
#[doc(inline)] pub use outcome::Outcome;
|
||||
#[doc(inline)] pub use data::Data;
|
||||
#[doc(inline)] pub use config::Config;
|
||||
#[doc(inline)] pub use error::Error;
|
||||
pub use router::Route;
|
||||
pub use request::{Request, State};
|
||||
pub use catcher::Catcher;
|
||||
|
@ -13,7 +13,6 @@ use rocket::Rocket;
|
||||
use router::Route;
|
||||
use config::{Config, Limits};
|
||||
use http::uri::{Origin, Segments};
|
||||
use error::Error;
|
||||
use http::{Method, Header, HeaderMap, Cookies, CookieJar};
|
||||
use http::{RawStr, ContentType, Accept, MediaType};
|
||||
use http::hyper;
|
||||
@ -554,9 +553,9 @@ impl<'r> Request<'r> {
|
||||
}
|
||||
|
||||
/// Retrieves and parses into `T` the 0-indexed `n`th dynamic parameter from
|
||||
/// the request. Returns `Error::NoKey` if `n` is greater than the number of
|
||||
/// params. Returns `Error::BadParse` if the parameter type `T` can't be
|
||||
/// parsed from the parameter.
|
||||
/// the request. Returns `None` if `n` is greater than the number of params.
|
||||
/// Returns `Some(Err(T::Error))` if the parameter type `T` failed to be
|
||||
/// parsed from the `n`th dynamic parameter.
|
||||
///
|
||||
/// This method exists only to be used by manual routing. To retrieve
|
||||
/// parameters from a request, use Rocket's code generation facilities.
|
||||
@ -572,12 +571,17 @@ impl<'r> Request<'r> {
|
||||
///
|
||||
/// # #[allow(dead_code)]
|
||||
/// fn name<'a>(req: &'a Request, _: Data) -> Outcome<'a> {
|
||||
/// Outcome::from(req, req.get_param::<String>(0).unwrap_or("unnamed".into()))
|
||||
/// let string = req.get_param::<String>(0)
|
||||
/// .and_then(|res| res.ok())
|
||||
/// .unwrap_or_else(|| "unnamed".into());
|
||||
///
|
||||
/// Outcome::from(req, string)
|
||||
/// }
|
||||
/// ```
|
||||
pub fn get_param<'a, T: FromParam<'a>>(&'a self, n: usize) -> Result<T, Error> {
|
||||
let param = self.get_param_str(n).ok_or(Error::NoKey)?;
|
||||
T::from_param(param).map_err(|_| Error::BadParse)
|
||||
pub fn get_param<'a, T>(&'a self, n: usize) -> Option<Result<T, T::Error>>
|
||||
where T: FromParam<'a>
|
||||
{
|
||||
Some(T::from_param(self.get_param_str(n)?))
|
||||
}
|
||||
|
||||
/// Get the `n`th path parameter as a string, if it exists. This is used by
|
||||
@ -610,8 +614,8 @@ impl<'r> Request<'r> {
|
||||
///
|
||||
/// # Error
|
||||
///
|
||||
/// If there are less than `n` segments, returns an `Err` of `NoKey`. If
|
||||
/// parsing the segments failed, returns an `Err` of `BadParse`.
|
||||
/// If there are less than `n` segments, returns `None`. If parsing the
|
||||
/// segments failed, returns `Some(Err(T:Error))`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@ -619,10 +623,10 @@ impl<'r> Request<'r> {
|
||||
/// path for this request is `"/hello/<name>/i/<segs..>"`, then
|
||||
/// `request.get_segments::<T>(1)` will attempt to parse the segments
|
||||
/// `"am/here"` as type `T`.
|
||||
pub fn get_segments<'a, T: FromSegments<'a>>(&'a self, n: usize)
|
||||
-> Result<T, Error> {
|
||||
let segments = self.get_raw_segments(n).ok_or(Error::NoKey)?;
|
||||
T::from_segments(segments).map_err(|_| Error::BadParse)
|
||||
pub fn get_segments<'a, T>(&'a self, n: usize) -> Option<Result<T, T::Error>>
|
||||
where T: FromSegments<'a>
|
||||
{
|
||||
Some(T::from_segments(self.get_raw_segments(n)?))
|
||||
}
|
||||
|
||||
/// Get the segments beginning at the `n`th dynamic parameter, if they
|
||||
|
@ -19,7 +19,7 @@ use response::{Body, Response};
|
||||
use router::{Router, Route};
|
||||
use catcher::{self, Catcher};
|
||||
use outcome::Outcome;
|
||||
use error::{Error, LaunchError, LaunchErrorKind};
|
||||
use error::{LaunchError, LaunchErrorKind};
|
||||
use fairing::{Fairing, Fairings};
|
||||
|
||||
use http::{Method, Status, Header};
|
||||
@ -320,12 +320,11 @@ impl Rocket {
|
||||
});
|
||||
|
||||
// Dispatch to the user's catcher. If it fails, use the default 500.
|
||||
let error = Error::NoRoute;
|
||||
catcher.handle(error, req).unwrap_or_else(|err_status| {
|
||||
catcher.handle(req).unwrap_or_else(|err_status| {
|
||||
error_!("Catcher failed with status: {}!", err_status);
|
||||
warn_!("Using default 500 error catcher.");
|
||||
let default = self.default_catchers.get(&500).expect("Default 500");
|
||||
default.handle(error, req).expect("Default 500 response.")
|
||||
default.handle(req).expect("Default 500 response.")
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ extern crate rocket;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
use rocket::{catch, Request, State};
|
||||
use rocket::{catch, State};
|
||||
use rocket_contrib::{Json, JsonValue};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
@ -6,10 +6,9 @@ mod tests;
|
||||
use std::io;
|
||||
use std::fs::File;
|
||||
|
||||
use rocket::{Request, Handler, Route, Data, Catcher, Error};
|
||||
use rocket::{Request, Handler, Route, Data, Catcher};
|
||||
use rocket::http::{Status, RawStr};
|
||||
use rocket::response::{self, Responder};
|
||||
use rocket::response::status::Custom;
|
||||
use rocket::response::{self, Responder, status::Custom};
|
||||
use rocket::handler::Outcome;
|
||||
use rocket::outcome::IntoOutcome;
|
||||
use rocket::http::Method::*;
|
||||
@ -23,8 +22,11 @@ fn hi<'r>(req: &'r Request, _: Data) -> Outcome<'r> {
|
||||
}
|
||||
|
||||
fn name<'a>(req: &'a Request, _: Data) -> Outcome<'a> {
|
||||
let param = req.get_param::<&'a RawStr>(0);
|
||||
Outcome::from(req, param.map(|r| r.as_str()).unwrap_or("unnamed"))
|
||||
let param = req.get_param::<&'a RawStr>(0)
|
||||
.and_then(|res| res.ok())
|
||||
.unwrap_or("unnamed".into());
|
||||
|
||||
Outcome::from(req, param.as_str())
|
||||
}
|
||||
|
||||
fn echo_url<'r>(req: &'r Request, _: Data) -> Outcome<'r> {
|
||||
@ -60,7 +62,7 @@ fn get_upload<'r>(req: &'r Request, _: Data) -> Outcome<'r> {
|
||||
Outcome::from(req, File::open("/tmp/upload.txt").ok())
|
||||
}
|
||||
|
||||
fn not_found_handler<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
||||
fn not_found_handler<'r>(req: &'r Request) -> response::Result<'r> {
|
||||
let res = Custom(Status::NotFound, format!("Couldn't find: {}", req.uri()));
|
||||
res.respond_to(req)
|
||||
}
|
||||
@ -78,7 +80,10 @@ impl CustomHandler {
|
||||
|
||||
impl Handler for CustomHandler {
|
||||
fn handle<'r>(&self, req: &'r Request, data: Data) -> Outcome<'r> {
|
||||
let id = req.get_param::<&RawStr>(0).ok().or_forward(data)?;
|
||||
let id = req.get_param::<&RawStr>(0)
|
||||
.and_then(|res| res.ok())
|
||||
.or_forward(data)?;
|
||||
|
||||
Outcome::from(req, format!("{} - {}", self.data, id))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user