pax_global_header00006660000000000000000000000064133121202230014477gustar00rootroot0000000000000052 comment=3953ebb9e4a33287e67676a70a8ad72bbbe38075 node-error-ex-1.3.2/000077500000000000000000000000001331212022300141705ustar00rootroot00000000000000node-error-ex-1.3.2/.editorconfig000066400000000000000000000004101331212022300166400ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.coffee] indent_style = space [{package.json,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false node-error-ex-1.3.2/.gitignore000066400000000000000000000001131331212022300161530ustar00rootroot00000000000000*.sw[a-p] /node_modules/ npm-debug.log /coverage/ yarn.lock yarn-error.log node-error-ex-1.3.2/.istanbul.yml000066400000000000000000000000731331212022300166120ustar00rootroot00000000000000instrumentation: excludes: - test.js - test/**/* node-error-ex-1.3.2/.travis.yml000066400000000000000000000004471331212022300163060ustar00rootroot00000000000000language: node_js script: - node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- --compilers coffee:coffee-script/register - cat coverage/lcov.info | node_modules/.bin/coveralls node_js: - "0.10" - "0.11" - "0.12" - "iojs" - "4" - "6" - "7" - "stable" os: - linux node-error-ex-1.3.2/LICENSE000066400000000000000000000020651331212022300152000ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 JD Ballard Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-error-ex-1.3.2/README.md000066400000000000000000000100571331212022300154520ustar00rootroot00000000000000# node-error-ex [![Travis-CI.org Build Status](https://img.shields.io/travis/Qix-/node-error-ex.svg?style=flat-square)](https://travis-ci.org/Qix-/node-error-ex) [![Coveralls.io Coverage Rating](https://img.shields.io/coveralls/Qix-/node-error-ex.svg?style=flat-square)](https://coveralls.io/r/Qix-/node-error-ex) > Easily subclass and customize new Error types ## Examples To include in your project: ```javascript var errorEx = require('error-ex'); ``` To create an error message type with a specific name (note, that `ErrorFn.name` will not reflect this): ```javascript var JSONError = errorEx('JSONError'); var err = new JSONError('error'); err.name; //-> JSONError throw err; //-> JSONError: error ``` To add a stack line: ```javascript var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')}); var err = new JSONError('error') err.fileName = '/a/b/c/foo.json'; throw err; //-> (line 2)-> in /a/b/c/foo.json ``` To append to the error message: ```javascript var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')}); var err = new JSONError('error'); err.fileName = '/a/b/c/foo.json'; throw err; //-> JSONError: error in /a/b/c/foo.json ``` ## API #### `errorEx([name], [properties])` Creates a new ErrorEx error type - `name`: the name of the new type (appears in the error message upon throw; defaults to `Error.name`) - `properties`: if supplied, used as a key/value dictionary of properties to use when building up the stack message. Keys are property names that are looked up on the error message, and then passed to function values. - `line`: if specified and is a function, return value is added as a stack entry (error-ex will indent for you). Passed the property value given the key. - `stack`: if specified and is a function, passed the value of the property using the key, and the raw stack lines as a second argument. Takes no return value (but the stack can be modified directly). - `message`: if specified and is a function, return value is used as new `.message` value upon get. Passed the property value of the property named by key, and the existing message is passed as the second argument as an array of lines (suitable for multi-line messages). Returns a constructor (Function) that can be used just like the regular Error constructor. ```javascript var errorEx = require('error-ex'); var BasicError = errorEx(); var NamedError = errorEx('NamedError'); // -- var AdvancedError = errorEx('AdvancedError', { foo: { line: function (value, stack) { if (value) { return 'bar ' + value; } return null; } } } var err = new AdvancedError('hello, world'); err.foo = 'baz'; throw err; /* AdvancedError: hello, world bar baz at tryReadme() (readme.js:20:1) */ ``` #### `errorEx.line(str)` Creates a stack line using a delimiter > This is a helper function. It is to be used in lieu of writing a value object > for `properties` values. - `str`: The string to create - Use the delimiter `%s` to specify where in the string the value should go ```javascript var errorEx = require('error-ex'); var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')}); var err = new FileError('problem reading file'); err.fileName = '/a/b/c/d/foo.js'; throw err; /* FileError: problem reading file in /a/b/c/d/foo.js at tryReadme() (readme.js:7:1) */ ``` #### `errorEx.append(str)` Appends to the `error.message` string > This is a helper function. It is to be used in lieu of writing a value object > for `properties` values. - `str`: The string to append - Use the delimiter `%s` to specify where in the string the value should go ```javascript var errorEx = require('error-ex'); var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')}); var err = new SyntaxError('improper indentation'); err.fileName = '/a/b/c/d/foo.js'; throw err; /* SyntaxError: improper indentation in /a/b/c/d/foo.js at tryReadme() (readme.js:7:1) */ ``` ## License Licensed under the [MIT License](http://opensource.org/licenses/MIT). You can find a copy of it in [LICENSE](LICENSE). node-error-ex-1.3.2/index.js000066400000000000000000000055331331212022300156430ustar00rootroot00000000000000'use strict'; var util = require('util'); var isArrayish = require('is-arrayish'); var errorEx = function errorEx(name, properties) { if (!name || name.constructor !== String) { properties = name || {}; name = Error.name; } var errorExError = function ErrorEXError(message) { if (!this) { return new ErrorEXError(message); } message = message instanceof Error ? message.message : (message || this.message); Error.call(this, message); Error.captureStackTrace(this, errorExError); this.name = name; Object.defineProperty(this, 'message', { configurable: true, enumerable: false, get: function () { var newMessage = message.split(/\r?\n/g); for (var key in properties) { if (!properties.hasOwnProperty(key)) { continue; } var modifier = properties[key]; if ('message' in modifier) { newMessage = modifier.message(this[key], newMessage) || newMessage; if (!isArrayish(newMessage)) { newMessage = [newMessage]; } } } return newMessage.join('\n'); }, set: function (v) { message = v; } }); var overwrittenStack = null; var stackDescriptor = Object.getOwnPropertyDescriptor(this, 'stack'); var stackGetter = stackDescriptor.get; var stackValue = stackDescriptor.value; delete stackDescriptor.value; delete stackDescriptor.writable; stackDescriptor.set = function (newstack) { overwrittenStack = newstack; }; stackDescriptor.get = function () { var stack = (overwrittenStack || ((stackGetter) ? stackGetter.call(this) : stackValue)).split(/\r?\n+/g); // starting in Node 7, the stack builder caches the message. // just replace it. if (!overwrittenStack) { stack[0] = this.name + ': ' + this.message; } var lineCount = 1; for (var key in properties) { if (!properties.hasOwnProperty(key)) { continue; } var modifier = properties[key]; if ('line' in modifier) { var line = modifier.line(this[key]); if (line) { stack.splice(lineCount++, 0, ' ' + line); } } if ('stack' in modifier) { modifier.stack(this[key], stack); } } return stack.join('\n'); }; Object.defineProperty(this, 'stack', stackDescriptor); }; if (Object.setPrototypeOf) { Object.setPrototypeOf(errorExError.prototype, Error.prototype); Object.setPrototypeOf(errorExError, Error); } else { util.inherits(errorExError, Error); } return errorExError; }; errorEx.append = function (str, def) { return { message: function (v, message) { v = v || def; if (v) { message[0] += ' ' + str.replace('%s', v.toString()); } return message; } }; }; errorEx.line = function (str, def) { return { line: function (v) { v = v || def; if (v) { return str.replace('%s', v.toString()); } return null; } }; }; module.exports = errorEx; node-error-ex-1.3.2/package.json000066400000000000000000000016141331212022300164600ustar00rootroot00000000000000{ "name": "error-ex", "description": "Easy error subclassing and stack customization", "version": "1.3.2", "maintainers": [ "Josh Junon (github.com/qix-)", "Sindre Sorhus (sindresorhus.com)" ], "keywords": [ "error", "errors", "extend", "extending", "extension", "subclass", "stack", "custom" ], "license": "MIT", "scripts": { "pretest": "xo", "test": "mocha --compilers coffee:coffee-script/register" }, "xo": { "rules": { "operator-linebreak": [ 0 ] } }, "repository": "qix-/node-error-ex", "files": [ "index.js" ], "devDependencies": { "coffee-script": "^1.9.3", "coveralls": "^2.11.2", "istanbul": "^0.3.17", "mocha": "^2.2.5", "should": "^7.0.1", "xo": "^0.7.1" }, "dependencies": { "is-arrayish": "^0.2.1" } } node-error-ex-1.3.2/test/000077500000000000000000000000001331212022300151475ustar00rootroot00000000000000node-error-ex-1.3.2/test/test.coffee000066400000000000000000000137151331212022300173060ustar00rootroot00000000000000should = require 'should' errorEx = require '../' Error.stackTraceLimit = Infinity it 'should create a default error type', -> TestError = errorEx() err = new TestError 'herp derp' err.should.be.instanceOf TestError err.should.be.instanceOf Error err.name.should.equal Error.name err.message.should.equal 'herp derp' it 'should create a new error type', -> TestError = errorEx 'TestError' err = new TestError 'herp derp' err.should.be.instanceOf TestError err.should.be.instanceOf Error err.name.should.equal 'TestError' testLine = err.stack.toString().split(/\r?\n/g)[0] testLine.should.equal 'TestError: herp derp' it 'should add a custom property line', -> TestError = errorEx 'TestError', foo:line: -> 'bar' err = new TestError 'herp derp' testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' bar' it 'should allow properties', -> TestError = errorEx 'TestError', foo:line: (v)-> "foo #{v}" if v err = new TestError 'herp derp' testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.substr(0, 3).should.not.equal 'foo' err.foo = 'bar' testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' foo bar' it 'should allow direct editing of the stack', -> TestError = errorEx 'TestError', foo:stack: (v, stack)-> stack[0] += " #{v}" if v err = new TestError 'herp derp' err.foo = 'magerp' testLine = err.stack.toString().split(/\r?\n/g)[0] testLine.should.equal 'TestError: herp derp magerp' it 'should work on existing errors', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError', foo:line: (v)-> "foo #{v}" TestError.call originalErr originalErr.message.should.equal 'herp derp' originalErr.name.should.equal 'TestError' originalErr.foo = 'bar' testLine = originalErr.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' foo bar' it 'should take in an existing error to the constructor', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError' newErr = new TestError originalErr newErr.message.should.equal originalErr.message it 'should allow the editing of the message', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError', foo:message: ()-> 'foobar' TestError.call originalErr originalErr.message.should.equal 'foobar' it 'should allow the editing of the message (with value)', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError', foo:message: (v)-> "foobar #{v}" TestError.call originalErr originalErr.foo = '1234' originalErr.message.should.equal 'foobar 1234' it 'should allow the editing of the message (multiple lines)', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError', foo:message: (v)-> ['hello', "foobar #{v}"] TestError.call originalErr originalErr.foo = '1234' originalErr.message.should.equal 'hello\nfoobar 1234' it 'should allow the editing of the message (append original)', -> originalErr = new Error 'herp derp' TestError = errorEx 'TestError', foo:message: (v, message)-> message.concat ['hello, there'] TestError.call originalErr originalErr.message.should.equal 'herp derp\nhello, there' describe 'helpers', -> describe 'append', -> it 'should append to the error string', -> TestError = errorEx 'TestError', fileName: errorEx.append 'in %s' err = new TestError 'error' err.fileName = '/a/b/c/foo.txt' testLine = err.stack.toString().split(/\r?\n/g)[0] testLine.should.equal 'TestError: error in /a/b/c/foo.txt' it 'should append to a multi-line error string', -> TestError = errorEx 'TestError', fileName: errorEx.append 'in %s' err = new TestError 'error\n}\n^' err.fileName = '/a/b/c/foo.txt' testLine = err.stack.toString().split(/\r?\n/g)[0] testLine.should.equal 'TestError: error in /a/b/c/foo.txt' err.message.should.equal 'error in /a/b/c/foo.txt\n}\n^' it 'should append and use toString()', -> TestError = errorEx 'TestError', fileName: errorEx.append 'in %s' err = new TestError 'error' err.fileName = '/a/b/c/foo.txt' err.toString().should.equal 'TestError: error in /a/b/c/foo.txt' err.message.should.equal 'error in /a/b/c/foo.txt' it 'should append and use toString() on existing error', -> TestError = errorEx 'TestError', fileName: errorEx.append 'in %s' err = new Error 'error' TestError.call err err.fileName = '/a/b/c/foo.txt' err.toString().should.equal 'TestError: error in /a/b/c/foo.txt' err.message.should.equal 'error in /a/b/c/foo.txt' describe 'line', -> it 'should create a new line', -> TestError = errorEx 'TestError', fileName: errorEx.line 'in %s' err = new TestError 'error' err.fileName = '/a/b/c/foo.txt' testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' in /a/b/c/foo.txt' describe 'bluebird support', -> it 'should pass the bluebird stack write test', -> bluebirdPropertyWritable = (obj, prop)-> descriptor = Object.getOwnPropertyDescriptor(obj, prop) return !!(!descriptor || descriptor.writable || descriptor.set) TestError = errorEx 'TestError', fileName: errorEx.line 'in %s' err = new TestError 'error' err_native = new Error 'hello' (bluebirdPropertyWritable err_native, 'stack').should.be.ok() (bluebirdPropertyWritable err, 'stack').should.be.ok() it 'should allow the stack to be set while preserving custom properties', -> TestError = errorEx 'TestError', fileName: errorEx.line 'in %s' err = new TestError 'error' err.fileName = '/a/b/c/foo.txt' testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' in /a/b/c/foo.txt' err.stack = 'TestError: overridden error\n at null:1:1' err.stack.toString().should.equal 'TestError: overridden error\n in /a/b/c/foo.txt\n at null:1:1' err.stack = null testLine = err.stack.toString().split(/\r?\n/g)[1] testLine.should.equal ' in /a/b/c/foo.txt'