<commit_before>/*
 * errs.js: Simple error creation and passing utilities.
 *
 * (C) 2012, Nodejitsu Inc.
 * MIT LICENSE
 *
 */

var events = require('events'),
    util = require('util');

//
// Container for registered error types.
//
exports.registered = {};

//
// Add `Error.prototype.toJSON` if it doesn't exist.
//
if (!Error.prototype.toJSON) {
  Object.defineProperty(Error.prototype, 'toJSON', {
    enumerable: false,
    value: function () {
      return mixin({
        message: this.message,
        stack: this.stack,
        arguments: this.arguments,
        type: this.type
      }, this);
    }
  });
}

//
// ### function create (type, opts)
// #### @type {string} **Optional** Registered error type to create
// #### @opts {string|object|Array|function} Options for creating the error:
//   * `string`:   Message for the error
//   * `object`:   Properties to include on the error
//   * `array`:    Message for the error (' ' joined).
//   * `function`: Function to return error options.
//
// Creates a new error instance for with the specified `type`
// and `options`. If the `type` is not registered then a new
// `Error` instance will be created.
//
exports.create = function createErr(type, opts) {
  if (!arguments[1] && !exports.registered[type]) {
    opts = type;
    type = null;
  }

  //
  // If the `opts` has a `stack` property assume
  // that it is already an error instance.
  //
  if (opts && opts.stack) {
    return opts;
  }

  var message,
      ErrorProto,
      error;

  //
  // Parse arguments liberally for the message
  //
  if (typeof opts === 'function') {
    opts = opts();
  }

  if (Array.isArray(opts)) {
    message = opts.join(' ');
    opts = null;
  }
  else if (opts) {
    switch (typeof opts) {
      case 'string':
        message = opts || 'Unspecified error';
        opts = null;
        break;
      case 'object':
        message = (opts && opts.message) || 'Unspecified error';
        break;
      default:
        message = 'Unspecified error';
        break;
    }
  }

  //
  // Instantiate a new Error instance or a new
  // registered error type (if it exists).
  //
  ErrorProto = type && exports.registered[type] || Error;
  error = new (ErrorProto)(message);

  if (!error.name || error.name === 'Error') {
    error.name = (opts && opts.name) || ErrorProto.name || 'Error';
  }

  //
  // Capture a stack trace if it does not already exist and
  // remote the part of the stack trace referencing `errs.js`.
  //
  if (!error.stack) {
    Error.call(error);
    Error.captureStackTrace(error, createErr);
  }
  else {
    error.stack = error.stack.split('\n');
    error.stack.splice(1, 1);
    error.stack = error.stack.join('\n');
  }

  //
  // Copy all options to the new error instance.
  //
  if (opts) {
    Object.keys(opts).forEach(function (key) {
      error[key] = opts[key];
    });
  }

  return error;
};

//
// ### function merge (err, type, opts)
// #### @err  {error} The error to merge
// #### @type {string} **Optional** Registered error type to create
// #### @opts {string|object|Array|function} Options for creating the error:
//   * `string`:   Message for the error
//   * `object`:   Properties to include on the error
//   * `array`:    Message for the error (' ' joined).
//   * `function`: Function to return error options.
//
// Merges an existing error with a new error instance for with
// the specified `type` and `options`.
//
exports.merge = function (err, type, opts) {
  var merged = exports.create(type, opts);

  //
  // optional stuff that might be created by module
  //
  Object.keys(err).forEach(function (key) {
    //
    // in node v0.4 v8 errors where treated differently
    // we need to make sure we aren't merging these properties
    // http://code.google.com/p/v8/issues/detail?id=1215
    //
    if (['stack', 'type', 'arguments', 'message'].indexOf(key)===-1) {
      merged[key] = err[key];
    }
  });

  // merging
  merged.name = merged.name || err.name;
  merged.message = merged.message || err.message;

  // override stack
  merged.stack = err.stack;

  // add human-readable errors
  merged.description = err.message;
  merged.stacktrace = err.stack.split("\n");

  return merged;
};

//
// ### function handle (error, callback)
// #### @error {string|function|Array|object} Error to handle
// #### @callback {function|EventEmitter} **Optional** Continuation or stream to pass the error to.<commit_msg>[fix] check for err.stack's existence in merge().<commit_after>/*
 * errs.js: Simple error creation and passing utilities.
 *
 * (C) 2012, Nodejitsu Inc.
 * MIT LICENSE
 *
 */

var events = require('events'),
    util = require('util');

//
// Container for registered error types.
//
exports.registered = {};

//
// Add `Error.prototype.toJSON` if it doesn't exist.
//
if (!Error.prototype.toJSON) {
  Object.defineProperty(Error.prototype, 'toJSON', {
    enumerable: false,
    value: function () {
      return mixin({
        message: this.message,
        stack: this.stack,
        arguments: this.arguments,
        type: this.type
      }, this);
    }
  });
}

//
// ### function create (type, opts)
// #### @type {string} **Optional** Registered error type to create
// #### @opts {string|object|Array|function} Options for creating the error:
//   * `string`:   Message for the error
//   * `object`:   Properties to include on the error
//   * `array`:    Message for the error (' ' joined).
//   * `function`: Function to return error options.
//
// Creates a new error instance for with the specified `type`
// and `options`. If the `type` is not registered then a new
// `Error` instance will be created.
//
exports.create = function createErr(type, opts) {
  if (!arguments[1] && !exports.registered[type]) {
    opts = type;
    type = null;
  }

  //
  // If the `opts` has a `stack` property assume
  // that it is already an error instance.
  //
  if (opts && opts.stack) {
    return opts;
  }

  var message,
      ErrorProto,
      error;

  //
  // Parse arguments liberally for the message
  //
  if (typeof opts === 'function') {
    opts = opts();
  }

  if (Array.isArray(opts)) {
    message = opts.join(' ');
    opts = null;
  }
  else if (opts) {
    switch (typeof opts) {
      case 'string':
        message = opts || 'Unspecified error';
        opts = null;
        break;
      case 'object':
        message = (opts && opts.message) || 'Unspecified error';
        break;
      default:
        message = 'Unspecified error';
        break;
    }
  }

  //
  // Instantiate a new Error instance or a new
  // registered error type (if it exists).
  //
  ErrorProto = type && exports.registered[type] || Error;
  error = new (ErrorProto)(message);

  if (!error.name || error.name === 'Error') {
    error.name = (opts && opts.name) || ErrorProto.name || 'Error';
  }

  //
  // Capture a stack trace if it does not already exist and
  // remote the part of the stack trace referencing `errs.js`.
  //
  if (!error.stack) {
    Error.call(error);
    Error.captureStackTrace(error, createErr);
  }
  else {
    error.stack = error.stack.split('\n');
    error.stack.splice(1, 1);
    error.stack = error.stack.join('\n');
  }

  //
  // Copy all options to the new error instance.
  //
  if (opts) {
    Object.keys(opts).forEach(function (key) {
      error[key] = opts[key];
    });
  }

  return error;
};

//
// ### function merge (err, type, opts)
// #### @err  {error} The error to merge
// #### @type {string} **Optional** Registered error type to create
// #### @opts {string|object|Array|function} Options for creating the error:
//   * `string`:   Message for the error
//   * `object`:   Properties to include on the error
//   * `array`:    Message for the error (' ' joined).
//   * `function`: Function to return error options.
//
// Merges an existing error with a new error instance for with
// the specified `type` and `options`.
//
exports.merge = function (err, type, opts) {
  var merged = exports.create(type, opts);

  //
  // optional stuff that might be created by module
  //
  Object.keys(err).forEach(function (key) {
    //
    // in node v0.4 v8 errors where treated differently
    // we need to make sure we aren't merging these properties
    // http://code.google.com/p/v8/issues/detail?id=1215
    //
    if (['stack', 'type', 'arguments', 'message'].indexOf(key)===-1) {
      merged[key] = err[key];
    }
  });

  // merging
  merged.name = merged.name || err.name;
  merged.message = merged.message || err.message;

  // override stack
  merged.stack = err.stack;

  // add human-readable errors
  merged.description = err.message;
  merged.stacktrace = (err.stack || '').split("\n");

  return merged;
};

//
// ### function handle (error, callback)
// #### @error {string|function|Array|object} Error to handle
// #### @callback {function|EventEmitter} **Optional** Continuation or stream to pass the error to.
{'fraction_non_alphanumeric': 0.15771211103648983, 'fraction_numerical': 0.003581822252070741, 'mean_word_length': 2.5811623246492985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4278627', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2735, 'n_words': 864}