Skip to content

Commit dfe6093

Browse files
committed
Don't loop forever if the template fails to render
Instead, panic, which only fails one request instead of sending the server into an infinite loop. This shouldn't ever happen in practice since error pages are tested in CI, but it's nice just in case. The panic looks like this: ``` 2021/06/21 00:05:07 [ERROR] docs_rs::web::page::web_page: called ctry!() on an `Err` value: Error { kind: Msg("Failed to render 'error.html'"), source: Some(Error { kind: Msg("Variable `oops` not found in context while rendering 'error.html'"), source: None }) } note: while attempting to fetch the route Url { generic_url: "http://localhost:3000/not_here" } 4: docs_rs::web::page::web_page::WebPage::into_response<docs_rs::web::ErrorPage> at src\web\page\web_page.rs:74 5: docs_rs::web::error::{{impl}}::handle at src\web\error.rs:78 6: iron::middleware::{{impl}}::handle at C:\Users\Joshua Nelson\.cargo\registry\src\github.com-1ecc6299db9ec823\iron-0.6.1\src\middleware\mod.rs:395 10: docs_rs::web::{{impl}}::handle::{{closure}} at src\web\mod.rs:241 11: enum$<core::result::Result<iron::response::Response, iron::error::IronError>>::or_else<iron::response::Response,iron::error::IronError,iron::error::IronError,closure-2> at /rustc/cc77ba46fcb2d288aa01554b48cd586c5827c3dd\library\core\src\result.rs:770 12: docs_rs::web::{{impl}}::handle at src\web\mod.rs:183 13: iron::iron::{{impl}}::handle<docs_rs::web::MainHandler> at C:\Users\Joshua Nelson\.cargo\registry\src\github.com-1ecc6299db9ec823\iron-0.6.1\src\iron.rs:176 thread '<unnamed>' panicked at 'error while serving error page: Error { kind: Msg("Failed to render 'error.html'"), source: Some(Error { kind: Msg("Variable `oops` not found in context while rendering 'error.html'"), source: None }) }', src\web\page\web_page.rs:72:20 ```
1 parent 7b80116 commit dfe6093

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/web/page/web_page.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,23 @@ pub trait WebPage: Serialize + Sized {
5959
page: &self,
6060
})
6161
.unwrap();
62-
let rendered = ctry!(
63-
req,
64-
req.extensions
65-
.get::<TemplateData>()
66-
.expect("missing TemplateData from the request extensions")
67-
.templates
68-
.load()
69-
.render(&self.template(), &ctx)
70-
);
62+
let status = self.get_status();
63+
let result = req
64+
.extensions
65+
.get::<TemplateData>()
66+
.expect("missing TemplateData from the request extensions")
67+
.templates
68+
.load()
69+
.render(&self.template(), &ctx);
70+
71+
let rendered = if status.is_server_error() {
72+
// avoid infinite loop if error.html somehow fails to load
73+
result.expect("error while serving error page")
74+
} else {
75+
ctry!(req, result)
76+
};
7177

72-
let mut response = Response::with((self.get_status(), rendered));
78+
let mut response = Response::with((status, rendered));
7379
response.headers.set(Self::content_type());
7480

7581
Ok(response)

0 commit comments

Comments
 (0)