From 9a9d07f0449c0924bb0ce8c2b8fed296dd1650f0 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 9 Sep 2016 01:00:51 -0700 Subject: [PATCH] Improve default catcher error messages. --- lib/src/catcher.rs | 80 ++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/lib/src/catcher.rs b/lib/src/catcher.rs index 1c8c83ed..7a31d93e 100644 --- a/lib/src/catcher.rs +++ b/lib/src/catcher.rs @@ -11,7 +11,7 @@ use term_painter::Color::*; pub struct Catcher { pub code: u16, handler: ErrorHandler, - is_default: bool + is_default: bool, } // TODO: Should `Catcher` be an interface? Should there be an `ErrorHandler` @@ -23,15 +23,15 @@ impl Catcher { Catcher::new_with_default(code, handler, false) } - pub fn handle<'r>(&self, error: Error, request: &'r Request<'r>) -> Response<'r> { - (self.handler)(error, request) + pub fn handle<'r>(&self, err: Error, request: &'r Request<'r>) -> Response<'r> { + (self.handler)(err, request) } fn new_with_default(code: u16, handler: ErrorHandler, default: bool) -> Catcher { Catcher { code: code, handler: handler, - is_default: default + is_default: default, } } @@ -57,42 +57,54 @@ pub mod defaults { use response::{StatusCode, Response, HTML}; use super::Catcher; use error::Error; - use std::collections::HashMap; + use std::collections::HashMap; pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> { - Response::with_status(StatusCode::NotFound, HTML("\ - \ - \ - 404: Not Found\ - \ - \ -

404: Not Found

\ -

The page you were looking for could not be found.

\ -


\ - Rocket\ - \ - ")) + Response::with_status(StatusCode::NotFound, 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<'r>) - -> Response<'r> { - Response::with_status(StatusCode::InternalServerError, HTML("\ - \ - \ - 404: Not Found\ - \ - \ -

500: Internal Server Error

\ -

The server encountered a problem processing your request.

\ -


\ - Rocket\ - ")) + pub fn internal_error<'r>(_error: Error, + _request: &'r Request<'r>) + -> Response<'r> { + Response::with_status(StatusCode::InternalServerError, 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 + 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 } }