all files / lib/offshore/error/ WLError.js

85% Statements 34/40
85% Branches 17/20
66.67% Functions 6/9
87.18% Lines 34/39
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                     84×     84× 84×       84× 44×   40×       84×         84×     83× 44×     39×       84× 49×                                                                                                                                                                
var util = require('util');
var _ = require('lodash');
 
/**
 * WLError
 *
 * All errors passed to a query callback in Offshore extend
 * from this base error class.
 *
 * @param  {Object} properties
 * @constructor {WLError}
 */
function WLError(properties) {
  WLError.super_.call(this);
 
  // Fold defined properties into the new WLError instance.
  properties || (properties = { });
  _.extend(this, properties);
 
  // Generate stack trace
  // (or use `originalError` if it is a true error instance)
  if (_.isObject(this.originalError) && this.originalError instanceof Error) {
    this._e = this.originalError;
  } else {
    this._e = new Error();
  }
 
  // Doctor up a modified version of the stack trace called `rawStack`:
  this.rawStack = (this._e.stack.replace(/^Error(\r|\n)*(\r|\n)*/, ''));
 
  // Customize `details`:
  // Try to dress up the wrapped "original" error as much as possible.
  // @type {String} a detailed explanation of this error
  if (_.isString(this.originalError)) {
    this.details = this.originalError;
 
  // Run toString() on Errors:
  } else if (this.originalError && util.isError(this.originalError)) {
    this.details = this.originalError.toString();
 
  // But for other objects, use util.inspect()
  } else if (this.originalError) {
    this.details = util.inspect(this.originalError);
  }
 
  // If `details` is set, prepend it with "Details:"
  if (this.details) {
    this.details = 'Details:  ' + this.details + '\n';
  }
}
 
util.inherits(WLError, Error);
 
// Default properties
WLError.prototype.status = 500;
WLError.prototype.code = 'E_UNKNOWN';
WLError.prototype.reason = 'Encountered an unexpected error';
WLError.prototype.details = '';
 
/**
 * Override JSON serialization.
 * (i.e. when this error is passed to `res.json()` or `JSON.stringify`)
 *
 * For example:
 * ```json
 * {
 *   status: 500,
 *   code: 'E_UNKNOWN'
 * }
 * ```
 *
 * @return {Object}
 */
WLError.prototype.toJSON =
WLError.prototype.toPOJO =
function() {
  var obj = {
    error: this.code,
    status: this.status,
    summary: this.reason,
    raw: this.originalError
  };
 
  // Only include `raw` if its truthy.
  if (!obj.raw) delete obj.raw;
 
  return obj;
};
 
/**
 * Override output for `sails.log[.*]`
 *
 * @return {String}
 *
 * For example:
 * ```sh
 * Offshore: ORM encountered an unexpected error:
 * { ValidationError: { name: [ [Object], [Object] ] } }
 * ```
 */
WLError.prototype.toLog = function() {
  return this.inspect();
};
 
/**
 * Override output for `util.inspect`
 * (also when this error is logged using `console.log`)
 *
 * @return {String}
 */
WLError.prototype.inspect = function() {
  return util.format('Error (%s) :: %s\n%s\n\n%s', this.code, this.reason, this.rawStack, this.details);
};
 
/**
 * @return {String}
 */
WLError.prototype.toString = function() {
  return util.format('[Error (%s) %s]', this.code, this.reason, this.details);
};
 
Object.defineProperties(WLError.prototype, {
  stack: {
    enumerable: true,
    get: function() {
      return util.format('Error (%s) :: %s\n%s', this.code, this.reason, this.rawStack);
    },
    set: function(value) {
      this.rawStack = value;
    }
  },
  message: {
    enumerable: true,
    get: function() {
      return this.rawMessage || this.toString();
    },
    set: function(value) {
      this.rawMessage = value;
    }
  }
});
 
module.exports = WLError;