Skip to content

Commit c25efb0

Browse files
committed
Website: don't crash server if EJS code crashes
Show the error message in the browser instead of crashing the server if an EJS script throws an exception.
1 parent 5509ab7 commit c25efb0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

website/src/server.mjs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ export function makeServer({ htmlRedirects, wwwRootPath }) {
3030
return;
3131

3232
case "build-ejs":
33+
let out = null;
34+
try {
35+
out = await router.renderEJSFile(
36+
path.join(router.wwwRootPath, classifiedDirectory.path)
37+
);
38+
} catch (error) {
39+
response.writeHeader(500, { "content-type": "text/plain" });
40+
response.end(error.stack);
41+
return;
42+
}
3343
response.writeHeader(200, { "content-type": "text/html" });
34-
let out = await router.renderEJSFile(
35-
path.join(router.wwwRootPath, classifiedDirectory.path)
36-
);
3744
response.end(out);
3845
return;
3946

website/test/test-server.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ describe("server", () => {
334334
);
335335
});
336336
});
337+
338+
describe("EJS", () => {
339+
it("exception causes 500 error", async () => {
340+
fs.writeFileSync(
341+
path.join(wwwRootPath, "index.ejs.html"),
342+
"<%= variableDoesNotExist %>"
343+
);
344+
345+
let response = await request.get("/");
346+
expect(response.status).toBe(500);
347+
expect(response.data).toContain("variableDoesNotExist");
348+
});
349+
});
337350
});
338351

339352
// quick-lint-js finds bugs in JavaScript programs.

0 commit comments

Comments
 (0)