1 | ;
|
2 |
|
3 | /**
|
4 | * Return error status and payload as response.
|
5 | *
|
6 | * @param {Response} res use as res.error(...)
|
7 | * @param {string|object} [message='Failure'] error message to return; if a strig is specified, it will be converted to an error object
|
8 | * @param {string} [audit=false] audit event type
|
9 | * @param {string} [extra=undefined] extra audit payload which may help debug event
|
10 | */
|
11 | function error(res, message = 'Failure', audit = false, extra = undefined)
|
12 | {
|
13 | if (audit)
|
14 | {
|
15 | res.audit(audit, message, extra);
|
16 | }
|
17 | if (typeof message === 'string')
|
18 | {
|
19 | message = {
|
20 | error: message
|
21 | };
|
22 | }
|
23 | res.status(400)
|
24 | .json(message);
|
25 | }
|
26 |
|
27 | module.exports = error;
|