All files / node-user-accounts-boilerplate-nahid/helper error.js

100% Statements 6/6
100% Branches 7/7
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        20x   13x   20x   19x       20x       5x  
"use strict";
 
/**
 * Return error status and payload as response.
 * 
 * @param {Response} res use as res.error(...)
 * @param {string|object} [message='Failure'] error message to return; if a strig is specified, it will be converted to an error object
 * @param {string} [audit=false] audit event type
 * @param {string} [extra=undefined] extra audit payload which may help debug event
 */
function error(res, message = 'Failure', audit = false, extra = undefined)
{
  if (audit)
  {
    res.audit(audit, message, extra);
  }
  if (typeof message === 'string')
  {
    message = {
      error: message
    };
  }
  res.status(400)
    .json(message);
}
 
module.exports = error;