diff --git a/contrib/lib/src/static_files.rs b/contrib/lib/src/static_files.rs index 15c005f3..395c050d 100644 --- a/contrib/lib/src/static_files.rs +++ b/contrib/lib/src/static_files.rs @@ -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::(0).ok() + let path = req.get_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)?; diff --git a/core/codegen_next/src/attribute/catch.rs b/core/codegen_next/src/attribute/catch.rs index 29a91dc4..11347efc 100644 --- a/core/codegen_next/src/attribute/catch.rs +++ b/core/codegen_next/src/attribute/catch.rs @@ -83,7 +83,6 @@ pub fn _catch(args: TokenStream, input: TokenStream) -> Result { #user_catcher_fn #vis fn #generated_fn_name<'_b>( - _: ::rocket::Error, __req: &'_b ::rocket::Request ) -> ::rocket::response::Result<'_b> { let response = #catcher_response; diff --git a/core/lib/src/catcher.rs b/core/lib/src/catcher.rs index 331ce83c..de50d6bd 100644 --- a/core/lib/src/catcher.rs +++ b/core/lib/src/catcher.rs @@ -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 { default_catchers! { diff --git a/core/lib/src/error.rs b/core/lib/src/error.rs index 853f9da6..ce6d3afa 100644 --- a/core/lib/src/error.rs +++ b/core/lib/src/error.rs @@ -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; diff --git a/core/lib/src/handler.rs b/core/lib/src/handler.rs index ab76b1bd..3770f85e 100644 --- a/core/lib/src/handler.rs +++ b/core/lib/src/handler.rs @@ -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 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`. diff --git a/core/lib/src/lib.rs b/core/lib/src/lib.rs index 7ab7be30..758b75de 100644 --- a/core/lib/src/lib.rs +++ b/core/lib/src/lib.rs @@ -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; diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index d8be7a09..b92b47fb 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -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::(0).unwrap_or("unnamed".into())) + /// let string = req.get_param::(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 { - 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> + 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//i/"`, then /// `request.get_segments::(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 { - 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> + 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 diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index e10a127a..e7383482 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -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.") }) } diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index dcc31f1a..5912ee92 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -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; diff --git a/examples/manual_routes/src/main.rs b/examples/manual_routes/src/main.rs index dacb9bff..6906d467 100644 --- a/examples/manual_routes/src/main.rs +++ b/examples/manual_routes/src/main.rs @@ -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)) } }