Skip to content

Commit

Permalink
fix(options): introduce custom opts.mapper function
Browse files Browse the repository at this point in the history
resolves #3
  • Loading branch information
tunnckoCore committed Mar 14, 2017
1 parent c21c24d commit 66bb5f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const stacktraceMetadata = require('stacktrace-metadata')

## API

### [stacktraceMetadata](index.js#L68)
### [stacktraceMetadata](index.js#L69)
> Cleans stack trace and attaches few more metadata properties, such as `at`, `line`, `column`, `filename` and `place`. By default it cleans stack, makes is short (4 length) and makes paths relative. But all this is controllable through `options` object. Throws `TypeError` if `error` is not an instance of Error.
**Params**
Expand All @@ -71,6 +71,7 @@ const stacktraceMetadata = require('stacktrace-metadata')
* `options.shortStack` **{Boolean}**: if `false` full stack traces, otherwise they are just four
* `options.showStack` **{Boolean}**: if `false` the error.stack will be empty string
* `options.relativePaths` **{Boolean}**: if `false` paths in stack traces will be absolute
* `options.mapper` **{Function}**: called on each line of the stack with `(line, index)` signature
* `options.cwd` **{String}**: current working directory, default `process.cwd()`
* `returns` **{Error}**: same error object, but modified

Expand Down
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var extend = require('extend-shallow')
var findCallsite = require('find-callsite')
var clean = require('clean-stacktrace')
var metadata = require('clean-stacktrace-metadata')
var relative = require('clean-stacktrace-relative-paths')
var relativePaths = require('clean-stacktrace-relative-paths')

/**
* > Cleans stack trace and attaches few more metadata properties,
Expand Down Expand Up @@ -59,6 +59,7 @@ var relative = require('clean-stacktrace-relative-paths')
* @param {Boolean} `options.shortStack` if `false` full stack traces, otherwise they are just four
* @param {Boolean} `options.showStack` if `false` the error.stack will be empty string
* @param {Boolean} `options.relativePaths` if `false` paths in stack traces will be absolute
* @param {Function} `options.mapper` called on each line of the stack with `(line, index)` signature
* @param {String} `options.cwd` current working directory, default `process.cwd()`
* @return {Error} same error object, but modified
* @throws {TypeError} If `error` not instance of Error
Expand All @@ -78,8 +79,10 @@ module.exports = function stacktraceMetadata (error, options) {
relativePaths: true
}, options)

var at = findCallsite(error)
at = opts.relativePaths ? relative(opts.cwd)(at) : at
var relative = function relative (val) {
return opts.relativePaths ? relativePaths(opts.cwd)(val) : val
}
var at = relative(findCallsite(error))

metadata(function (_, info) {
error.at = [
Expand All @@ -99,8 +102,13 @@ module.exports = function stacktraceMetadata (error, options) {
error.filename = info.filename
})(at)

var mapper = opts.relativePaths ? relative(opts.cwd) : null
var stack = clean(error.stack, mapper)
var stack = clean(error.stack, function mapper (line, index) {
line = typeof opts.mapper === 'function'
? (opts.mapper(line, index) || line)
: line
line = relative(line)
return line
})

if (opts.showStack) {
error.stack = opts.cleanStack ? stack : error.stack
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ test('should work for errors thrown like what rimraf.sync throws', function quxi
try {
rimraf.sync(12345)
} catch (err) {
var e = stacktraceMetadata(err)
var e = stacktraceMetadata(err, {
mapper: function () {}
})

test.strictEqual(e.line, 174)
test.strictEqual(e.column, 12)
Expand All @@ -184,3 +186,16 @@ test('should work for errors thrown like what rimraf.sync throws', function quxi
}
done()
})

test('should be able to pass custom opts.mapper function', function (done) {
var err = new Error('custom mapper')
var e = stacktraceMetadata(err, {
mapper: function (line) {
test.strictEqual(typeof line, 'string')
return line
}
})

test.strictEqual(e.stack.trim().length > 0, true)
done()
})

0 comments on commit 66bb5f5

Please sign in to comment.