/** * Support utilities for implementing error responses */ 'use strict'; var _ = require('lodash'); var debug = require('debug')('webconsole-api:responses'); var auth = require(global.pathPrefix + 'auth.js'); /** * Constant value for the default response if we don't fund something more specific */ const DEFAULT_RESPONSE = { httpCode: 500, // INTERNAL SERVER ERROR bodyCode: -1, bodyDesc: 'Unspecified error' }; /** * A class to handle making error responses. * * @class */ module.exports.ErrorResponses = class ErrorResponses { /** * Creates an instance of ErrorResponses, based on a table of errors. * This table should be an array of arrays. Each item in the overall array * should have the following params in order: * [0] - The ID of the error in the error being handled * [1] - The http response code for this error * [2] - The code in the JSON response * [3] - The description in the JSON response * [4] - [OPTIONAL] true = the response is in `error.name` rather than the top level * * @param {any[]} responses - the responses table * * @memberOf ErrorResponses */ constructor(responses) { this.baseResponses = {}; this.nameResponses = {}; const ERROR_ID = 0; const HTTP_CODE = 1; const BODY_CODE = 2; const BODY_DESC = 3; const IS_NAME = 4; for (let i = 0; i < responses.length; ++i) { let response = responses[i]; let table = this.baseResponses; if (response[IS_NAME]) { table = this.nameResponses; } table[response[ERROR_ID]] = { httpCode: response[HTTP_CODE], bodyCode: response[BODY_CODE], bodyDesc: response[BODY_DESC] }; } } /** * Responds to the WEB caller with an appropriate error message based on the * error info. * * @param {Object} res - the express response handler * @param {any} error - the error info */ respond(res, error) { debug('Request Error', error); let response = this.findResponse(error); res.status(response.httpCode).json({ code: response.bodyCode, info: response.bodyDesc }); } /** * Responds to the APP caller with an appropriate error message based on the * error info. * * @param {Object} res - the express response handler * @param {any} error - the error info * @param {any} device - the device the app is running on * @param {any} hmacData - data for calculating HMAC * @param {any} functionInfo - info about the current function * @param {any} level - the level of notification to log */ respondAuth(res, error, device, hmacData, functionInfo, level) { let response = this.findResponse(error); auth.respond(res, response.httpCode, device, hmacData, functionInfo, { code: '' + response.bodyCode, info: response.bodyDesc }, level ); } /** * Find the response from our table of responses * * @param {any} error - the error to lookup * @returns {Object} - the response to use */ findResponse(error) { let response = DEFAULT_RESPONSE; if (error.hasOwnProperty('name')) { if (this.nameResponses.hasOwnProperty(error.name)) { response = this.nameResponses[error.name]; } } else if (_.isString(error)) { if (this.baseResponses.hasOwnProperty(error)) { response = this.baseResponses[error]; } } return response; } };