Improve default catcher error messages.

This commit is contained in:
Sergio Benitez 2016-09-09 01:00:51 -07:00
parent 8fee2bbf38
commit 9a9d07f044
1 changed files with 46 additions and 34 deletions

View File

@ -11,7 +11,7 @@ use term_painter::Color::*;
pub struct Catcher { pub struct Catcher {
pub code: u16, pub code: u16,
handler: ErrorHandler, handler: ErrorHandler,
is_default: bool is_default: bool,
} }
// TODO: Should `Catcher` be an interface? Should there be an `ErrorHandler` // TODO: Should `Catcher` be an interface? Should there be an `ErrorHandler`
@ -23,15 +23,15 @@ impl Catcher {
Catcher::new_with_default(code, handler, false) Catcher::new_with_default(code, handler, false)
} }
pub fn handle<'r>(&self, error: Error, request: &'r Request<'r>) -> Response<'r> { pub fn handle<'r>(&self, err: Error, request: &'r Request<'r>) -> Response<'r> {
(self.handler)(error, request) (self.handler)(err, request)
} }
fn new_with_default(code: u16, handler: ErrorHandler, default: bool) -> Catcher { fn new_with_default(code: u16, handler: ErrorHandler, default: bool) -> Catcher {
Catcher { Catcher {
code: code, code: code,
handler: handler, handler: handler,
is_default: default is_default: default,
} }
} }
@ -57,42 +57,54 @@ pub mod defaults {
use response::{StatusCode, Response, HTML}; use response::{StatusCode, Response, HTML};
use super::Catcher; use super::Catcher;
use error::Error; use error::Error;
use std::collections::HashMap; use std::collections::HashMap;
pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> { pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> {
Response::with_status(StatusCode::NotFound, HTML("\ Response::with_status(StatusCode::NotFound, HTML(r#"
<head>\ <!DOCTYPE html>
<meta charset=\"utf-8\">\ <html>
<title>404: Not Found</title>\ <head>
</head>\ <meta charset="utf-8">
<body align=\"center\">\ <title>404: Not Found</title>
<h1>404: Not Found</h1>\ </head>
<p>The page you were looking for could not be found.<p>\ <body>
<hr />\ <div align="center">
<small>Rocket</small>\ <h1>404: Not Found</h1>
</body>\ <p>The page you were looking for could not be found.<p>
")) <hr />
<small>Rocket</small>
</div>
</body>
</html>
"#))
} }
pub fn internal_error<'r>(_error: Error, _request: &'r Request<'r>) pub fn internal_error<'r>(_error: Error,
-> Response<'r> { _request: &'r Request<'r>)
Response::with_status(StatusCode::InternalServerError, HTML("\ -> Response<'r> {
<head>\ Response::with_status(StatusCode::InternalServerError, HTML(r#"
<meta charset=\"utf-8\">\ <!DOCTYPE html>
<title>404: Not Found</title>\ <html>
</head>\ <head>
<body align=\"center\">\ <meta charset="utf-8">
<h1>500: Internal Server Error</h1>\ <title>500: Internal Server Error</title>
<p>The server encountered a problem processing your request.<p>\ </head>
<hr />\ <body align="center">
<small>Rocket</small>\ <div align="center">
")) <h1>500: Internal Server Error</h1>
<p>The server encountered a problem processing your request.<p>
<hr />
<small>Rocket</small>
</div>
</body>
</html>
"#))
} }
pub fn get() -> HashMap<u16, Catcher> { pub fn get() -> HashMap<u16, Catcher> {
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert(404, Catcher::new_with_default(404, not_found, true)); map.insert(404, Catcher::new_with_default(404, not_found, true));
map.insert(500, Catcher::new_with_default(500, internal_error, true)); map.insert(500, Catcher::new_with_default(500, internal_error, true));
map map
} }
} }