forked from XGovFormBuilder/digital-form-builder
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patherrorPages.ts
More file actions
61 lines (54 loc) · 1.74 KB
/
Copy patherrorPages.ts
File metadata and controls
61 lines (54 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { HapiRequest, HapiResponseToolkit, HapiServer } from "../types";
import config from "../config";
/*
* Add an `onPreResponse` listener to return error pages
*/
export default {
plugin: {
name: "error-pages",
register: (server: HapiServer) => {
server.ext(
"onPreResponse",
(request: HapiRequest, h: HapiResponseToolkit) => {
const response = request.response;
if ("isBoom" in response && response.isBoom) {
// An error was raised during
// processing the request
const statusCode = response.output.statusCode;
// In the event of 404
// return the `404` view
if (statusCode === 404) {
return h.view("404").code(statusCode);
}
request.log("error", {
statusCode: statusCode,
data: response.data,
message: response.message,
});
try {
const url = request.url;
var urlPath = url.pathname.split("/");
var form = server.app.forms[urlPath[1]];
} catch (e) {
return h.view("500").code(500);
}
// In the event of 403 (CSRF protection)
if (statusCode === 403) {
return h
.view("csrf-protection", { url: urlPath[1], name: form.name })
.code(statusCode);
}
// The return the `500` view
return h
.view("500", {
name: form?.name || config.serviceName,
contactEmail: form?.def.error500ContactEmail,
})
.code(statusCode);
}
return h.continue;
}
);
},
},
};