Wrap a Mongo error document in an Error instance - Node.js Data Type

Node.js examples for Data Type:Error

Description

Wrap a Mongo error document in an Error instance

Demo Code


/**/*from  ww  w  . j  a  v a 2s.  c  o m*/
 * Wrap a Mongo error document in an Error instance
 * @ignore
 * @api private
 */
exports.toError = function(error) {
  if (error instanceof Error) return error;

  var msg = error.err || error.errmsg || error;
  var e = new Error(msg);
  e.name = 'MongoError';

  // Get all object keys
  var keys = typeof error == 'object'
    ? Object.keys(error)
    : [];

  for(var i = 0; i < keys.length; i++) {
    e[keys[i]] = error[keys[i]];
  }

  return e;
}

Related Tutorials