Comparing version 0.1.2 to 0.2.0
43
index.js
@@ -11,3 +11,3 @@ 'use strict'; | ||
if (options instanceof Error) | ||
throw new Error('Stackman not initialized yet. Please do so first and use the returned function instead'); | ||
throw new Error('Stackman not initialized yet. Please do so first and parse the error to the returned function instead'); | ||
@@ -22,3 +22,6 @@ var lines_of_context = (options || {}).context || LINES_OF_CONTEXT; | ||
next = afterAll(function () { | ||
callback(stack); | ||
callback({ | ||
properties: getProperties(err), | ||
frames: stack | ||
}); | ||
}); | ||
@@ -89,5 +92,21 @@ | ||
var getCulprit = function () { | ||
// module | ||
// file | ||
// function/object/method | ||
// | ||
// ModuleName: file.function | ||
// ModuleName: Type.method | ||
var filename; | ||
if (this.isNode()) { | ||
filename = this.getFileName(); | ||
} else { | ||
} | ||
var module = this.getModuleName(); | ||
}; | ||
var getModuleName = function () { | ||
var filename = this.getFileName() || ''; | ||
var match = filename.match(/node_modules\/([^\/]*)/); | ||
var match = filename.match(/.*node_modules\/([^\/]*)/); | ||
if (match) return match[1]; | ||
@@ -97,4 +116,3 @@ } | ||
var isApp = function () { | ||
var filename = this.getFileName() || ''; | ||
return !this.isNode() && !~filename.indexOf('node_modules/') | ||
return !this.isNode() && !~(this.getFileName() || '').indexOf('node_modules/') | ||
}; | ||
@@ -111,1 +129,16 @@ | ||
}; | ||
var getProperties = function (err) { | ||
var properties = {}; | ||
Object.keys(err).forEach(function (key) { | ||
var val = err[key]; | ||
switch (typeof val) { | ||
case 'function': | ||
case 'object': | ||
return; | ||
default: | ||
properties[key] = val; | ||
} | ||
}); | ||
return properties; | ||
}; |
{ | ||
"name": "stackman", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Enhance an error stacktrace with code excerpts and other goodies", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,4 +27,3 @@ # Stackman | ||
stackman(err, function (stack) { | ||
// stack is an array of callsite objects | ||
stack.forEach(function (frame) { | ||
stack.frames.forEach(function (frame) { | ||
// output: <example.js:3> var err = new Error('Oops!'); | ||
@@ -51,7 +50,20 @@ console.log('<%s:%s> %s', | ||
Options: | ||
- `context` - The lines of context to be loaded on each side of the callsite line (default: 7) | ||
The `stackman` function takes two arguments: | ||
- `err` - the error to be parsed | ||
- `callback` - a callback which will be called with an array of callsite stack-frames when the parsing is completed | ||
- `callback` - a callback which will be called with the a stack object when the parsing is completed | ||
#### The `stack` object: | ||
The callback given to the `stackman` function is called with a stack | ||
object when the parsing is completed. The `stack` object have two | ||
important properties: | ||
- `properties` - An object containing all the custom properties from the original error object (properties of type `object` and `function` are not included in this object) | ||
- `frames` - An array of stack-frames, also called callsite objects | ||
### Callsite | ||
@@ -69,3 +81,3 @@ | ||
- `callsite.getModuleName()` - Returns the name of the module if `isModule()` is true | ||
- `callsite.isApp()` - Is this inside the app? (i.e. not native, not node code and not a nodule inside the node_modules directory) | ||
- `callsite.isApp()` - Is this inside the app? (i.e. not native, not node code and not a module inside the node_modules directory) | ||
- `callsite.isModule()` - Is this inside the node_modules directory? | ||
@@ -72,0 +84,0 @@ - `callsite.isNode()` - Is this inside node core? |
30
test.js
'use strict'; | ||
var fs = require('fs'); | ||
var test = require('tape'); | ||
@@ -7,8 +8,10 @@ var afterAll = require('after-all'); | ||
test('should call the callback with an array of callsite objects', function (t) { | ||
test('should call the callback with a stack object', function (t) { | ||
var err = new Error(); | ||
stackman()(err, function (stack) { | ||
t.ok(Array.isArray(stack), 'should be an array'); | ||
t.ok(stack.length > 0, 'should have at least one element'); | ||
t.equal(typeof stack[0].getFileName, 'function'); | ||
t.ok(typeof stack === 'object', 'should be an object'); | ||
t.ok(typeof stack.properties === 'object', 'should be an object'); | ||
t.ok(Array.isArray(stack.frames), 'should be an array'); | ||
t.ok(stack.frames.length > 0, 'should have at least one element'); | ||
t.equal(typeof stack.frames[0].getFileName, 'function'); | ||
t.end(); | ||
@@ -18,6 +21,15 @@ }); | ||
test('should add custom properties to the stack object', function (t) { | ||
fs.readFile('./no_such_file', function (err) { | ||
stackman()(err, function (stack) { | ||
t.equal(stack.properties, { errno: 34, code: 'ENOENT', path: './no_such_file' }), | ||
t.end(); | ||
}); | ||
}); | ||
}); | ||
test('should add extra functions', function (t) { | ||
var err = new Error(); | ||
stackman()(err, function (stack) { | ||
var frame = stack[0]; | ||
var frame = stack.frames[0]; | ||
t.equal(typeof frame.getFunctionNameSanitized, 'function'); | ||
@@ -35,3 +47,3 @@ t.equal(typeof frame.getModuleName, 'function'); | ||
stackman()(err, function (stack) { | ||
var frame = stack[0]; | ||
var frame = stack.frames[0]; | ||
t.equal(typeof frame.context, 'object'); | ||
@@ -50,3 +62,3 @@ t.equal(typeof frame.context.line, 'string'); | ||
stackman({ context: 2 })(err, function (stack) { | ||
var frame = stack[0]; | ||
var frame = stack.frames[0]; | ||
t.equal(frame.context.pre.length, 2); | ||
@@ -64,3 +76,3 @@ t.equal(frame.context.post.length, 2); | ||
stackman({ context: 1 })(err, function (stack) { | ||
var frame = stack[0]; | ||
var frame = stack.frames[0]; | ||
t.equal(frame.context.pre.length, 1); | ||
@@ -71,3 +83,3 @@ t.equal(frame.context.post.length, 1); | ||
stackman({ context: 2 })(err, function (stack) { | ||
var frame = stack[0]; | ||
var frame = stack.frames[0]; | ||
t.equal(frame.context.pre.length, 2); | ||
@@ -74,0 +86,0 @@ t.equal(frame.context.post.length, 2); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12846
191
105
2