-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorHandler.js
38 lines (34 loc) · 1.16 KB
/
errorHandler.js
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
/* eslint-disable space-before-function-paren */
const Validator = require("./validator");
module.exports = {
/*
* This function is intended to prepare a response with a JSON error payload
* based on a Javascript error that reaches the top of the call stack.
*/
prepareResponse(context, error) {
const out = {
status: 500,
body: { error: "An error occurred" },
headers: {
"Content-Type": "application/json",
},
};
// Check to see if this is a validation errors object.
if (error instanceof Validator.Errors) {
out.status = 400;
out.body.error = "Validation error(s)";
out.body.validator_errors = error.all();
} else if (error instanceof Error) {
context.error(error.stack);
// This was previusly defualting to null if there was no status
// However, that was preventing the function from returning details about the error.
out.status = error?.status || 500;
out.body.error = error.message;
} else {
out.body.error = error; // assume this is a simple string
}
out.body = JSON.stringify(out.body);
context.log("out", out);
return out;
},
};