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

100% Statements 16/16
100% Branches 11/11
100% Functions 3/3
100% Lines 15/15
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                                                              84×   59×                             59×                   57×                         59×           57×                        
/**
 * Module dependencies
 */
 
var util = require('util');
var _ = require('lodash');
var WLError = require('./WLError');
var WLValidationError = require('./WLValidationError');
 
 
/**
 * A classifier which normalizes a mystery error into a simple,
 * consistent format.  This ensures that our instance which is
 * "new"-ed up belongs to one of a handful of distinct categories
 * and has a predictable method signature and properties.
 *
 * The returned error instance will always be or extend from
 * `WLError` (which extends from `Error`)
 *
 * NOTE:
 * This method should eventually be deprecated in a
 * future version of Offshore.  It exists to help
 * w/ error type negotiation.  In general, Offshore
 * should use WLError, or errors which extend from it
 * to construct error objects of the appropriate type.
 * In other words, no ** new ** errors should need to
 * be wrapped in a call to `errorify` - instead, code
 * necessary to handle any new error conditions should
 * construct a `WLError` directly and return that.
 *
 * @param  {???} err
 * @return {WLError}
 */
module.exports = function errorify(err) {
 
  // If specified `err` is already a WLError, just return it.
  if (typeof err === 'object' && err instanceof WLError) return err;
 
  return duckType(err);
};
 
 
/**
 * Determine which type of error we're working with.
 * Err... using hacks.
 *
 * @return {[type]} [description]
 */
function duckType(err) {
 
  // Validation or constraint violation error (`E_VALIDATION`)
  //
  // i.e. detected before talking to adapter, like `minLength`
  // i.e. constraint violation reported by adapter, like `unique`
  if (/* _isValidationError(err) || */ _isConstraintViolation(err)) {
 
    // Dress `unique` rule violations to be consistent with other
    // validation errors.
    return new WLValidationError(err);
  }
 
  // Unexpected miscellaneous error  (`E_UNKNOWN`)
  //
  // (i.e. helmet fire. The database crashed or something. Or there's an adapter
  //  bug. Or a bug in WL core.)
  return new WLError({
    originalError: err
  });
}
 
 
/**
 * @param  {?} err
 * @return {Boolean} whether this is an adapter-level constraint
 * violation (e.g. `unique`)
 */
function _isConstraintViolation(err) {
 
  // If a proper error code is specified, this error can be classified.
  if (err && typeof err === 'object' && err.code === 'E_UNIQUE') {
    return true;
  }
 
  // Otherwise, there is not enough information to call this a
  // constraint violation error and provide proper explanation to
  // the architect.
  else return false;
}
 
 
// /**
//  * @param  {?} err
//  * @return {Boolean} whether this is a validation error (e.g. minLength exceeded for attribute)
//  */
// function _isValidationError(err) {
//   return _.isObject(err) && err.ValidationError;
// }