AriaMinaei / pretty-error

See node.js errors with less clutter
MIT License
1.52k stars 49 forks source link

Feature Request: skipReverse #28

Closed reggi closed 9 years ago

reggi commented 9 years ago

Because of the nature of skip, it runs a loop from top (latest) line in the stack to the bottom (earilier) line in the stack. This limited control is preventing me from doing something I think is rather simple.

I'd like to specify a file in a stack and get everything from the first call (last instance) up.

So given this stack of addrs.

/Users/thomas/Desktop/node-reggi/delta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js
/Users/thomas/Desktop/node-reggi/alpha.js

If I specify beta.js I'd like to skip alpha.js.

I have this working here.

//skips every addr after `node.js` (from top)
var found = false
pe.skip(function (traceLine) {
  var _found = found
  var after = 'beta.js'
  var pattern = new RegExp(after)
  var match = traceLine.path.match(pattern)
  if (match) found = true
  return _found
})

Where this fails is if beta is called twice in the stack

/Users/thomas/Desktop/node-reggi/delta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js
/Users/thomas/Desktop/node-reggi/alpha.js

This function will return

/Users/thomas/Desktop/node-reggi/delta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js

instead of this, which is my desired result.

/Users/thomas/Desktop/node-reggi/delta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js
/Users/thomas/Desktop/node-reggi/gamma..js
/Users/thomas/Desktop/node-reggi/beta.js

If there was a skipReverse function I could do this pretty easily. Is there any way to do this?

reggi commented 9 years ago

Or Ideally pe.skip had a this reference to the entire parsedError.

reggi commented 9 years ago

Here's a sad implementation

function reverseLines(err, callback) {
  var parsedError = new ParsedError(err)
  var traceLines = parsedError.trace
  var traceLinesSkip = _.chain(traceLines)
  .reverse()
  .map(function (traceLine, lineNumber) {
    traceLine.skip = callback(traceLine, lineNumber)
    return traceLine
  })
  .reverse()
  .value()
  return function (traceLine, lineNumber) {
    return traceLinesSkip[lineNumber].skip
  }
}

pe.skip(reverseLines(e, function (traceLine, lineNumber) {
  console.log(traceLine.path)
}))
reggi commented 9 years ago

With this

var found = false
pe.skip(reverseLines(e, function (traceLine, lineNumber) {
  var after = 'module.js'
  var pattern = new RegExp(after)
  var match = traceLine.file.match(pattern)
  if (match) found = true
  return !found
}))

This:

Error: Hello World
    at Object.<anonymous> (/Users/thomas/Desktop/node-reggi/z-default-stack.js:49:9)
    at Module._compile (module.js:430:26)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:471:10)
    at startup (node.js:117:18)
    at node.js:953:3

becomes this:

Error: Hello World
    at Object.<anonymous> (/Users/thomas/Desktop/node-reggi/z-default-stack.js:49:9)
    at Module._compile (module.js:430:26)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:471:10)
AriaMinaei commented 9 years ago

There is an undocumented method called PrettyError::filterParsedError(fn). fn takes a ParsedError instance and can modify it any way it sees fit.

AriaMinaei commented 9 years ago

Feel free to close this issue if you think it has been resolved.

reggi commented 9 years ago

:+1: