0.2.0 • Published 5 years ago

nn-node-stacktrace v0.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

Node stack trace utils

Node stack trace callsites and some other tiny utils to play with.

NPM version NPM monthly download

  • Caution: This package uses some private APIs from Node, those APIs are not guaranteed to be kept overtime. It's also a new package under heavy developing and has not been well tested yet. If any problem happens, feel free to open an issue.
  • Require:
    • Node >= v8.0.0

Reference

Example

Get a CallSite object

const stacktrace = require('nn-node-stacktrace')

try {
  throw new Error('boooo')
  // or put some of your code instead
} catch(err) {
  const csArr = stacktrace.callsites(err)
  // see https://github.com/v8/v8/wiki/Stack-Trace-API
  console.log(csArr[0].getFileName(), csArr[0].getLineNumber())
}

Clean stack output string

Imagine that we have catched an error, and we got the default stack output like this mess:

TypeError: model.toSomething is not a function
    at Object.exports.login (/Users/user/nnm/awesome-project/src/handlers/login.js:22:15)
    at main (/Users/user/nnm/awesome-project/src/server.js:37:20)
    at Object.<anonymous> (/Users/user/nnm/awesome-project/src/server.js:35:1)
    at Module._compile (module.js:571:32)
    at loader (/Users/user/nnm/awesome-project/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/user/nnm/awesome-project/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)

Now let's say that we only have interesting with our src folder. And here is the cleaned result from stacktrace.clean(err):

    at login        handlers/login.js:22
    at main         server.js:37
    at <anonnymous> server.js:50

API

stacktrace.setCwd(cwd)

  • Set the current working directory
  • Parameters:
    • cwd:
      • string - Required

stacktrace.callsites(err)

  • Get the structured stack trace from an error using V8 Stack Trace API. See the Customizing stack traces section in V8 Stack Trace API for more detail.
  • Parameters:
    • err:
      • Error - Required
      • An Error object to get the structured stack trace from.
  • Return:
    • An array of CallSite objects, which holds the raw information for all stack layers.

stacktrace.clean(err[, filterFn, filenameFn])

  • Get the cleaned stack trace output string from an error.
  • Parameters:
    • err:
      • Error - Required
      • An Error object to get the cleaned stack trace output string.
    • filterFn:
      • Function - Optional - Default view source
      • A function to filter the stack layer.
    • filenameFn:
      • Function - Optional - Default view source
      • A function to get the shortened file name from the stack layer.
  • Return:
    • A cleaned stack trace string output. An emptry string is returned if there's no layer matches.

stacktrace.location([n, filterFn, filenameFn])

  • Get the location using file name and line number at the nth call layer.
  • Parameters:
    • n:
      • number - Optional - Default 0
      • The index of call layer which we want to trace.
    • filterFn:
      • Function - Optional - Default view source
      • A function to filter the stack layer.
    • filenameFn:
      • Function - Optional - Default view source
      • A function to get the shortened file name from the stack layer.
  • Return:
    • A string concatenated from the file name and line number. An emptry string is returned if there's no layer at the nth index.

License

MIT

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.6

5 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago