Commit 371052a0 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Show HTTP error status codes as simple html pages

parent 4d93e3bc
......@@ -63,6 +63,7 @@ macro_rules! template_ok {
}};
}
/** Show error messages on commandline */
struct ErrorReporter;
impl AfterMiddleware for ErrorReporter {
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
......@@ -71,6 +72,30 @@ impl AfterMiddleware for ErrorReporter {
}
}
/** Show error messages to users */
struct ErrorShower;
impl AfterMiddleware for ErrorShower {
fn catch(&self, _: &mut Request, err: IronError) -> IronResult<Response> {
let IronError { error, response } = err;
if response.body.is_none() {
Ok(match response.status {
Some(s) => {
let n = s.to_u16();
if n >= 400 && n <= 599 {
response.set((mime!(Text / Html),
format!("<h1>{} {}</h1>", n, s.canonical_reason().unwrap_or("(Unknown error)"))))
} else {
response
}
}
_ => response,
})
} else {
Err(IronError { error, response })
}
}
}
#[derive(Debug)]
struct SessionToken {
token: String,
......@@ -957,6 +982,7 @@ pub fn start_server<C>(conn: C, config: Config) -> iron::error::HttpResult<iron:
ch.link_after(get_handlebars_engine(&config.template.unwrap_or_else(|| "default".to_string())));
ch.link_after(ErrorReporter);
ch.link_after(ErrorShower);
let socket_addr = format!("{}:{}", config.host.unwrap(), config.port.unwrap());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment