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,
}
}
@ -60,33 +60,45 @@ pub mod defaults {
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>)
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>\
"))
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> {