2016-08-26 08:55:11 +00:00
|
|
|
use handler::ErrorHandler;
|
2016-07-16 04:09:08 +00:00
|
|
|
use response::Response;
|
2016-04-06 10:26:43 +00:00
|
|
|
use codegen::StaticCatchInfo;
|
2016-08-26 08:55:11 +00:00
|
|
|
use error::Error;
|
|
|
|
use request::Request;
|
2016-04-06 10:26:43 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use term_painter::ToStyle;
|
|
|
|
use term_painter::Color::*;
|
|
|
|
|
|
|
|
pub struct Catcher {
|
|
|
|
pub code: u16,
|
2016-08-26 08:55:11 +00:00
|
|
|
handler: ErrorHandler,
|
2016-09-09 08:00:51 +00:00
|
|
|
is_default: bool,
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 04:09:08 +00:00
|
|
|
// 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?
|
|
|
|
|
2016-04-06 10:26:43 +00:00
|
|
|
impl Catcher {
|
2016-08-26 08:55:11 +00:00
|
|
|
pub fn new(code: u16, handler: ErrorHandler) -> Catcher {
|
2016-04-06 20:50:02 +00:00
|
|
|
Catcher::new_with_default(code, handler, false)
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:00:51 +00:00
|
|
|
pub fn handle<'r>(&self, err: Error, request: &'r Request<'r>) -> Response<'r> {
|
|
|
|
(self.handler)(err, request)
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:55:11 +00:00
|
|
|
fn new_with_default(code: u16, handler: ErrorHandler, default: bool) -> Catcher {
|
2016-04-06 10:26:43 +00:00
|
|
|
Catcher {
|
|
|
|
code: code,
|
|
|
|
handler: handler,
|
2016-09-09 08:00:51 +00:00
|
|
|
is_default: default,
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 20:50:02 +00:00
|
|
|
|
|
|
|
pub fn is_default(&self) -> bool {
|
|
|
|
self.is_default
|
|
|
|
}
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-04-06 20:50:02 +00:00
|
|
|
write!(f, "{}", Blue.paint(&self.code))
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 20:50:02 +00:00
|
|
|
|
|
|
|
pub mod defaults {
|
|
|
|
use request::Request;
|
2016-09-21 03:02:33 +00:00
|
|
|
use response::{StatusCode, Response};
|
|
|
|
use response::data;
|
2016-04-06 20:50:02 +00:00
|
|
|
use super::Catcher;
|
2016-08-26 08:55:11 +00:00
|
|
|
use error::Error;
|
2016-09-09 08:00:51 +00:00
|
|
|
use std::collections::HashMap;
|
2016-04-06 20:50:02 +00:00
|
|
|
|
2016-08-26 08:55:11 +00:00
|
|
|
pub fn not_found<'r>(_error: Error, _request: &'r Request<'r>) -> Response<'r> {
|
2016-09-21 03:02:33 +00:00
|
|
|
Response::with_status(StatusCode::NotFound, data::HTML(r#"
|
2016-09-09 08:00:51 +00:00
|
|
|
<!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>
|
|
|
|
"#))
|
2016-04-06 20:50:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:00:51 +00:00
|
|
|
pub fn internal_error<'r>(_error: Error,
|
|
|
|
_request: &'r Request<'r>)
|
|
|
|
-> Response<'r> {
|
2016-09-21 03:02:33 +00:00
|
|
|
Response::with_status(StatusCode::InternalServerError, data::HTML(r#"
|
2016-09-09 08:00:51 +00:00
|
|
|
<!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>
|
|
|
|
"#))
|
2016-04-06 20:50:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 04:09:08 +00:00
|
|
|
pub fn get() -> HashMap<u16, Catcher> {
|
2016-09-09 08:00:51 +00:00
|
|
|
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
|
2016-07-16 04:09:08 +00:00
|
|
|
}
|
|
|
|
}
|