use handler::ErrorHandler; use response::Response; use codegen::StaticCatchInfo; use error::Error; use request::Request; use std::fmt; use term_painter::ToStyle; use term_painter::Color::*; pub struct Catcher { pub code: u16, handler: ErrorHandler, is_default: bool, } // TODO: Should `Catcher` be an interface? Should there be an `ErrorHandler` // that takes in a `RoutingError` and returns a `Response`? What's the right // interface here? impl Catcher { pub fn new(code: u16, handler: ErrorHandler) -> Catcher { Catcher::new_with_default(code, handler, false) } pub fn handle<'b, 'r>(&self, err: Error, request: &'b Request<'r>) -> Response<'b> { (self.handler)(err, request) } fn new_with_default(code: u16, handler: ErrorHandler, default: bool) -> Catcher { Catcher { code: code, handler: handler, is_default: default, } } pub fn is_default(&self) -> bool { self.is_default } } impl<'a> From<&'a StaticCatchInfo> for Catcher { fn from(info: &'a StaticCatchInfo) -> Catcher { Catcher::new(info.code, info.handler) } } impl fmt::Display for Catcher { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", Blue.paint(&self.code)) } } pub mod defaults { use super::Catcher; use std::collections::HashMap; use request::Request; use response::Response; use response::data; use error::Error; use http::hyper::StatusCode; pub fn not_found<'r>(_error: Error, _request: &'r Request) -> Response<'r> { Response::with_status(StatusCode::NotFound, data::HTML(r#" 404: Not Found

404: Not Found

The page you were looking for could not be found.


Rocket
"#)) } pub fn internal_error<'r>(_error: Error, _request: &'r Request) -> Response<'r> { Response::with_status(StatusCode::InternalServerError, data::HTML(r#" 500: Internal Server Error

500: Internal Server Error

The server encountered a problem processing your request.


Rocket
"#)) } pub fn get() -> HashMap { let mut map = HashMap::new(); map.insert(404, Catcher::new_with_default(404, not_found, true)); map.insert(500, Catcher::new_with_default(500, internal_error, true)); map } }