[Pkg-javascript-commits] [node-error-ex] 01/03: Import Upstream version 1.3.0
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Wed Oct 26 07:28:25 UTC 2016
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-error-ex.
commit 581fc4c84a19425fde21415e2cad6480bcd1fe3b
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date: Wed Oct 26 12:36:52 2016 +0530
Import Upstream version 1.3.0
---
.editorconfig | 18 +++++++
.gitignore | 4 ++
.istanbul.yml | 4 ++
.travis.yml | 13 +++++
LICENSE | 21 ++++++++
README.md | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
index.js | 115 ++++++++++++++++++++++++++++++++++++++++++++
package.json | 46 ++++++++++++++++++
test/test.coffee | 120 ++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 485 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..4c017f8
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,18 @@
+root = 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
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9aef994
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.sw[a-p]
+/node_modules/
+npm-debug.log
+/coverage/
diff --git a/.istanbul.yml b/.istanbul.yml
new file mode 100644
index 0000000..19fbec3
--- /dev/null
+++ b/.istanbul.yml
@@ -0,0 +1,4 @@
+instrumentation:
+ excludes:
+ - test.js
+ - test/**/*
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..bbe1264
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,13 @@
+language: 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"
+os:
+ - linux
+ - osx
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..0a5f461
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..97f744a
--- /dev/null
+++ b/README.md
@@ -0,0 +1,144 @@
+# 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).
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..1bdc943
--- /dev/null
+++ b/index.js
@@ -0,0 +1,115 @@
+'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;
+
+ delete this.message;
+
+ 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) && 'message' in properties[key]) {
+ newMessage = properties[key].message(this[key], newMessage) ||
+ newMessage;
+ if (!isArrayish(newMessage)) {
+ newMessage = [newMessage];
+ }
+ }
+ }
+
+ return newMessage.join('\n');
+ },
+ set: function (v) {
+ message = v;
+ }
+ });
+
+ var stackDescriptor = Object.getOwnPropertyDescriptor(this, 'stack');
+ var stackGetter = stackDescriptor.get;
+
+ stackDescriptor.get = function () {
+ var stack = stackGetter.call(this).split(/\r?\n+/g);
+
+ 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);
+ };
+
+ 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;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..4582ea1
--- /dev/null
+++ b/package.json
@@ -0,0 +1,46 @@
+{
+ "name": "error-ex",
+ "description": "Easy error subclassing and stack customization",
+ "version": "1.3.0",
+ "maintainers": [
+ "JD Ballard <i.am.qix at gmail.com> (github.com/qix-)",
+ "Sindre Sorhus <sindresorhus at gmail.com> (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"
+ }
+}
diff --git a/test/test.coffee b/test/test.coffee
new file mode 100644
index 0000000..fccc6b8
--- /dev/null
+++ b/test/test.coffee
@@ -0,0 +1,120 @@
+should = 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'
+
+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'
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-error-ex.git
More information about the Pkg-javascript-commits
mailing list