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 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("\
<head>\
<meta charset=\"utf-8\">\
<title>404: Not Found</title>\
</head>\
<body align=\"center\">\
<h1>404: Not Found</h1>\
<p>The page you were looking for could not be found.<p>\
<hr />\
<small>Rocket</small>\
</body>\
"))
Response::with_status(StatusCode::NotFound, HTML(r#"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>404: Not Found</title>
</head>
<body>
<div align="center">
<h1>404: Not Found</h1>
<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>)
-> Response<'r> {
Response::with_status(StatusCode::InternalServerError, HTML("\
<head>\
<meta charset=\"utf-8\">\
<title>404: Not Found</title>\
</head>\
<body align=\"center\">\
<h1>500: Internal Server Error</h1>\
<p>The server encountered a problem processing your request.<p>\
<hr />\
<small>Rocket</small>\
"))
pub fn internal_error<'r>(_error: Error,
_request: &'r Request<'r>)
-> Response<'r> {
Response::with_status(StatusCode::InternalServerError, HTML(r#"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>500: Internal Server Error</title>
</head>
<body align="center">
<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> {
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
}
}