Returns failing code for given error
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
.npmignore
README.md
example.js
index.js
package.json
test.js

README.md

failing-code

Returns failing code for given error. It's based on failing-line.

Install

$ npm install failing-code

Usage

a.js:

require('./b')

var foo
hereIfail++;
var bar

b.js:

var failingCode = require('failing-code')

process.on('uncaughtException', function (error) {

  failingCode(error)
  // => [
  //      { line: 3, code: "var foo" },
  //      {
  //        line: 4,
  //        col: 0,
  //        code: "here I fail++",
  //        fn: "Object.<anonymous>",
  //        filename: "a.js"
  //      },
  //      { line: 5, code: "var bar" }
  //   ]

})

It reads the source code syncronously by default. If you'd like to avoid sync reading, you can read the file and pass it to failing-code as second parameter:

failingLine = require('failing-line')
ln = failingLine(error)

fs.readFile(ln.filename, function (error, buffer) {

  failingCode(error, buffer.toString())
  // => [...]

})

If you'd like to skip some lines from the top of the stack trace, pass the number of the lines to skip as third parameter:

failingCode(error, null, 1) // will get skip the first line of the stack

See test.js and failing-line for more info.