Skip to content

Commit 4bd01a2

Browse files
committed
fix: error message detail does not appear in default and custom error pages
Fixes #111
1 parent 84cc787 commit 4bd01a2

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

src/StaticFileHandler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ class StaticFileHandler {
175175
*/
176176
async responseAsError(errorText, statusCode) {
177177
const context = {
178-
errorText: errorText,
178+
staticFileHandler: {
179+
viewData: {
180+
errorText: errorText,
181+
},
182+
},
179183
}
180184
if (this.customErrorPagePath) {
181185
let filePath = path.join(this.clientFilesPath, this.customErrorPagePath)
@@ -199,7 +203,7 @@ class StaticFileHandler {
199203
</head>
200204
201205
<body>
202-
{errorText}
206+
{{errorText}}
203207
</body>
204208
</html>
205209
`

src/test/StaticFileHandler.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,59 @@ describe("StaticFileHandler", function () {
131131
})
132132
})
133133

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

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

155-
it("should use customErrorPagePath", async function () {
156-
const event = mockEvent({ path: "doesntexist.404" })
157-
let h = new StaticFileHandler(STATIC_FILES_PATH, "custom-error.html")
158-
const response = h.get(event, null)
159-
expect(response)
160-
.to.eventually.haveOwnProperty("statusCode")
161-
.that.equals(404)
162-
return expect(response)
163-
.to.eventually.haveOwnProperty("body")
164-
.that.matches(/<title>CUSTOM<\/title>/)
155+
it("should return 404 when no path parameters", function () {
156+
const event = mockEvent({ path: "doesntexist.404" })
157+
let h = new StaticFileHandler(STATIC_FILES_PATH)
158+
const response = h.get(event, null)
159+
return expect(response)
160+
.to.eventually.haveOwnProperty("statusCode")
161+
.that.equals(404)
162+
})
163+
164+
it("should return 404 customErrorPagePath is invalid", function () {
165+
const event = mockEvent({ path: "doesntexist.404" })
166+
let h = new StaticFileHandler(
167+
STATIC_FILES_PATH,
168+
"error-page-doesnt-exist-either.html"
169+
)
170+
const response = h.get(event, null)
171+
return expect(response)
172+
.to.eventually.haveOwnProperty("statusCode")
173+
.that.equals(404)
174+
})
175+
176+
it("should use customErrorPagePath", async function () {
177+
const event = mockEvent({ path: "doesntexist.404" })
178+
let h = new StaticFileHandler(STATIC_FILES_PATH, "custom-error.html")
179+
const response = h.get(event, null)
180+
expect(response)
181+
.to.eventually.haveOwnProperty("statusCode")
182+
.that.equals(404)
183+
return expect(response)
184+
.to.eventually.haveOwnProperty("body")
185+
.that.matches(/<title>CUSTOM<\/title>/)
186+
})
165187
})
166188

167189
/**

src/test/data/testfiles/custom-error.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
</head>
77

88
<body>
9-
CUSTOM {errorText}
9+
CUSTOM {{errorText}}
1010
</body>
1111
</html>

0 commit comments

Comments
 (0)