tunnckoCore/useful-error

View on GitHub
index.js

Summary

Maintainability
A
1 hr
Test Coverage
/*!
 * useful-error <https://github.com/tunnckoCore/useful-error>
 *
 * Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
 * Released under the MIT license.
 */

'use strict'

var util = require('util')
var utils = require('./utils')

module.exports = utils.errorBase('UsefulError', function (message, options) {
  if (typeof message === 'object') {
    options = message
    message = false
  }

  var format = options && typeof options.format === 'function' ? options.format : defaultFormat
  var opts = utils.extend({
    showStack: true,
    message: message && message.length && message || false
  }, options)

  opts.message = typeof opts.message === 'function'
    ? opts.message.call(this, opts)
    : (typeof opts.message === 'string' ? opts.message : '')

  utils.delegate(this, opts)
  utils.errorFormat(this, format)

  if (this.showStack === false && hasOwn(this, 'stack')) {
    delete this['stack']
  }
})

/**
 * > Default error `toString` method formatting.
 *
 * @param  {String} `headline`
 * @return {String}
 */

function defaultFormat (headline) {
  return util.format('%s (at %s:%s:%s)', headline, this.filename, this.line, this.column)
}

/**
 * > Has own property util.
 *
 * @param  {OBject}  `self`
 * @param  {String}  `key`
 * @return {Boolean}
 */

function hasOwn (self, key) {
  return Object.prototype.hasOwnProperty.call(self, key)
}