error-ex
Advanced tools
Comparing version 0.3.1 to 1.0.0
69
index.js
@@ -20,9 +20,29 @@ 'use strict'; | ||
Error.captureStackTrace(this, errorExError); | ||
this.message = message; | ||
this.name = name; | ||
var descriptor = Object.getOwnPropertyDescriptor(this, 'stack'); | ||
var stackGetter = descriptor.get; | ||
delete this.message; | ||
descriptor.get = function () { | ||
Object.defineProperty(this, 'message', { | ||
configurable: true, | ||
enumerable: false, | ||
get: function () { | ||
var newMessage = message; | ||
for (var key in properties) { | ||
if (properties.hasOwnProperty(key) && 'message' in properties[key]) { | ||
newMessage = properties[key].message(this[key], newMessage); | ||
} | ||
} | ||
return newMessage; | ||
}, | ||
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); | ||
@@ -37,6 +57,13 @@ | ||
var modifier = properties[key]; | ||
var line = modifier(this[key], stack); | ||
if (line) { | ||
stack.splice(lineCount, 0, ' ' + line); | ||
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); | ||
} | ||
} | ||
@@ -47,3 +74,3 @@ | ||
Object.defineProperty(this, 'stack', descriptor); | ||
Object.defineProperty(this, 'stack', stackDescriptor); | ||
}; | ||
@@ -57,6 +84,11 @@ | ||
errorEx.append = function (str, def) { | ||
return function (v, stack) { | ||
v = v || def; | ||
if (v) { | ||
stack[0] += ' ' + str.replace('%s', v.toString()); | ||
return { | ||
message: function (v, message) { | ||
v = v || def; | ||
if (v) { | ||
message += ' ' + str.replace('%s', v.toString()); | ||
} | ||
return message; | ||
} | ||
@@ -67,6 +99,11 @@ }; | ||
errorEx.line = function (str, def) { | ||
return function (v) { | ||
v = v || def; | ||
if (v) { | ||
return str.replace('%s', v.toString()); | ||
return { | ||
line: function (v) { | ||
v = v || def; | ||
if (v) { | ||
return str.replace('%s', v.toString()); | ||
} | ||
return null; | ||
} | ||
@@ -73,0 +110,0 @@ }; |
{ | ||
"name": "error-ex", | ||
"description": "Easy error subclassing and stack customization", | ||
"version": "0.3.1", | ||
"version": "1.0.0", | ||
"author": "Qix (http://github.com/qix-)", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -48,7 +48,11 @@ # 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) | ||
looked up on the error message, and then passed to function values. | ||
- Values are functions that are passed the property named by *key* as the | ||
first argument and the `.stack`, split into an array of lines, as the | ||
second argument | ||
- Functions that return falsey values are simply skipped | ||
- Stack is passed as to allow direct stack message modification, if needed | ||
- `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. | ||
@@ -68,7 +72,9 @@ Returns a constructor (Function) that can be used just like the regular Error | ||
var AdvancedError = errorEx('AdvancedError', { | ||
foo: function (value, stack) { | ||
if (value) { | ||
return 'bar ' + value; | ||
foo: { | ||
line: function (value, stack) { | ||
if (value) { | ||
return 'bar ' + value; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} | ||
@@ -91,4 +97,4 @@ } | ||
> This is a helper function. It is to be used in lieu of writing an anonymous | ||
> function for `properties` values. | ||
> This is a helper function. It is to be used in lieu of writing a value object | ||
> for `properties` values. | ||
@@ -117,4 +123,4 @@ - `str`: The string to create | ||
> This is a helper function. It is to be used in lieu of writing an anonymous | ||
> function for `properties` values. | ||
> This is a helper function. It is to be used in lieu of writing a value object | ||
> for `properties` values. | ||
@@ -121,0 +127,0 @@ - `str`: The string to append |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8510
82
0
144