AriaMinaei / pretty-error

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

Question: Selecting out variables? #22

Closed reggi closed 9 years ago

reggi commented 9 years ago

I'm looking for a way to pull out the lineNumber from an error like the one below.

SyntaxError: Unexpected token (210:6)

Can this be done using pretty-error?

PS. Love the library!

reggi commented 9 years ago

I really need a way of parsing the error getting the variables edit them and put the message back together. parse and format in the style of node's parse and url.

reggi commented 9 years ago

I was also hoping you'd have a standard "theme" just like node / console.

reggi commented 9 years ago

Ohhh, the line number in the message in specific to the message returned from acorn

AriaMinaei commented 9 years ago

Error#stack is one way to get the stack tract of that error. This doesn't need pretty-error, of course.

var error = new Error("Some error message");
var lines = error.stack;
console.log(lines);

Does this help?

reggi commented 9 years ago

@AriaMinaei Hey

I create a parseLineChar func to help me do what I need to do.

function parseLineChar (s) {
  var pattern = /(\d+):(\d+)/
  var match = s.match(pattern)
  if (match) {
    match.lineChar = match[0]
    match.line = match[1]
    match.char = match[2]
  }
  return false
}

Running this on acorn's e.message gets me what I need.

Given an error like this:

/Users/thomas/Desktop/node-reggi/docs/evalmd-alpha.js:3
assert.equals(false, true)
       ^
TypeError: assert.equals is not a function
    at Object.<anonymous> (/Users/thomas/Desktop/node-reggi/docs/evalmd-alpha.js:3:8)
    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

Still interested in better / standard (gonna work on all versions of node) version of these.

/** get the stack without the file lines */
function stackFrame (stack) {
  var stackLines = stack.split('\n')
  return _.chain(stackLines)
  .filter(function (stackLine) {
    var match = /^\s\s\s\sat\s/
    return !stackLine.match(match)
  })
  .value().join('\n')
}

/** get the line:char from string */
function stackTrace function (stack) {
  var stackLines = stack.split('\n')
  return _.chain(stackLines)
  .filter(function (stackLine) {
    var match = /^\s\s\s\sat\s/
    return stackLine.match(match)
  })
  .value()
}

Your lib must have these does it expose them?

{
  message: 'assert.equals is not a function'
  type: 'TypeError'
  frame: "/Users/thomas/Desktop/node-reggi/docs/evalmd-alpha.js:3\nassert.equals(false, true)\n       ^"
  trace: [
    'at Object.<anonymous> (/Users/thomas/Desktop/node-reggi/docs/evalmd-alpha.js:3:8)'
    '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'
  ]
}
AriaMinaei commented 9 years ago

Your lib must have these does it expose them?

It suer does :)

You can use it like this:

var ParsedError = require('pretty-error/lib/parsed-error');
var err = new Error("Some message");
var parsedError = new ParsedError(err);
console.log(parsedError); // This will give you all the parsed out pieces of that error
reggi commented 9 years ago

THIS IS AWESOME.

How can I get to these vars??

console.log(parsedError.error)
{ [SyntaxError: Unexpected token (210:6)] pos: 372, loc: { line: 210, column: 6 }, raisedAt: 374 }
console.log(parsedError.error.line) => undefined
reggi commented 9 years ago

Oh that's not you :)

All the props prefaced with _ are from parsedError.

AriaMinaei commented 9 years ago

Yeah. Although, you can just use parsedError.trace. It's a getter method. A sample output: image

Does that help?

reggi commented 9 years ago

Yep! Thanks a bunch!

AriaMinaei commented 9 years ago

Glad to be of help!