Skip to content

Commit

Permalink
fix: error message detail does not appear in default and custom error…
Browse files Browse the repository at this point in the history
… pages

Fixes #111
  • Loading branch information
activescott committed Sep 26, 2021
1 parent 84cc787 commit 4bd01a2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
8 changes: 6 additions & 2 deletions src/StaticFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ class StaticFileHandler {
*/
async responseAsError(errorText, statusCode) {
const context = {
errorText: errorText,
staticFileHandler: {
viewData: {
errorText: errorText,
},
},
}
if (this.customErrorPagePath) {
let filePath = path.join(this.clientFilesPath, this.customErrorPagePath)
Expand All @@ -199,7 +203,7 @@ class StaticFileHandler {
</head>
<body>
{errorText}
{{errorText}}
</body>
</html>
`
Expand Down
80 changes: 51 additions & 29 deletions src/test/StaticFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,37 +131,59 @@ describe("StaticFileHandler", function () {
})
})

it("should return 404 when no path parameters", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH)
const response = h.get(event, null)
return expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
})
describe("error response", function () {
it("should include viewData in default error page", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH)
const response = h.get(event, null)
// the default error page mentions page name and "does not exist"
return expect(response)
.to.eventually.haveOwnProperty("body")
.that.matches(/doesntexist\.404 does not exist/)
})

it("should return 404 customErrorPagePath is invalid", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(
STATIC_FILES_PATH,
"error-page-doesnt-exist-either.html"
)
const response = h.get(event, null)
return expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
})
it("should include viewData in custom error page", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH, "custom-error.html")
const response = h.get(event, null)
// the default error page mentions page name and "does not exist"
return expect(response)
.to.eventually.haveOwnProperty("body")
.that.matches(/doesntexist\.404 does not exist/)
})

it("should use customErrorPagePath", async function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH, "custom-error.html")
const response = h.get(event, null)
expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
return expect(response)
.to.eventually.haveOwnProperty("body")
.that.matches(/<title>CUSTOM<\/title>/)
it("should return 404 when no path parameters", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH)
const response = h.get(event, null)
return expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
})

it("should return 404 customErrorPagePath is invalid", function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(
STATIC_FILES_PATH,
"error-page-doesnt-exist-either.html"
)
const response = h.get(event, null)
return expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
})

it("should use customErrorPagePath", async function () {
const event = mockEvent({ path: "doesntexist.404" })
let h = new StaticFileHandler(STATIC_FILES_PATH, "custom-error.html")
const response = h.get(event, null)
expect(response)
.to.eventually.haveOwnProperty("statusCode")
.that.equals(404)
return expect(response)
.to.eventually.haveOwnProperty("body")
.that.matches(/<title>CUSTOM<\/title>/)
})
})

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/data/testfiles/custom-error.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</head>

<body>
CUSTOM {errorText}
CUSTOM {{errorText}}
</body>
</html>

0 comments on commit 4bd01a2

Please sign in to comment.