stacktracey
Parses call stacks, reads sources and meta-information.
Last updated 8 years ago by xpl .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install stacktracey 
SYNC missed versions from official npm registry.

StackTracey

Build Status Windows Build Status Coverage Status NPM Scrutinizer Code Quality dependencies Status

Parses call stacks. Reads sources. Clean & filtered output. Sourcemaps. Node & browsers.

Why

  • [x] Simple
  • [x] Works in Node and browsers, *nix and Windows
  • [x] Allows hiding library calls / ad-hoc exclusion (via // @hide marker)
  • [x] Provides source text for call locations
  • [x] Fetches sources (via get-source)
  • [x] Supports both asynchronous and synchronous interfaces (works even in browsers)
  • [x] Full sourcemap support
  • [x] Extracts useful information from SyntaxError instances
  • [x] Pretty printing screen shot 2017-09-27 at 16 53 46

What For

How To

npm install stacktracey
import StackTracey from 'stacktracey'

Captures the current call stack:

stack = new StackTracey ()            // captures the current call stack

Parses stacks from an Error object:

stack = new StackTracey (error)
stack = new StackTracey (error.stack) // ...or from raw string

Stores parsed data in .items:

stack.items.length // num entries
stack.items[0]     // top

...where each item exposes:

{
    beforeParse:  <original text>,
    callee:       <function name>,
    calleeShort:  <shortened function name>,
    file:         <full path to file>,       // e.g. /Users/john/my_project/node_modules/foobar/main.js
    fileRelative: <relative path to file>,   // e.g. node_modules/foobar/main.js
    fileShort:    <short path to file>,      // e.g. foobar/main.js
    fileName:     <file name>,               // e.g. main.js
    line:         <line number>,             // starts from 1
    column:       <column number>,           // starts from 1

    index:          /* true if occured in HTML file at index page    */,
    native:         /* true if occured in native browser code        */,
    thirdParty:     /* true if occured in library code               */,
    hide:           /* true if marked as hidden by "// @hide" tag    */,
    syntaxError:    /* true if generated from a SyntaxError instance */
}

Accessing sources (synchronously, use with caution in browsers):

stack = stack.withSources () // returns a copy of stack with all items supplied with sources
top   = stack.items[0]       // top item

Accessing sources (asynchronously, preferred method in browsers):

stack = await stack.withSourcesAsync () // returns a copy of stack with all items supplied with sources
top   = stack.items[0]                  // top item

...or:

top = stack.withSourceAt (0) // supplies source for an individiual item (by index)
top = await stack.withSourceAsyncAt (0) // supplies source for an individiual item (by index)

...or:

top = stack.withSource (stack.items[0]) // supplies source for an individiual item
top = await stack.withSourceAsync (stack.items[0]) // supplies source for an individiual item

The returned items contain the following additional fields (already mapped through sourcemaps):

{
    ... // all the previously described fields

    line:       <original line number>,
    column:     <original column number>,
    sourceFile: <original source file object>,
    sourceLine: <original source line text>
}

To learn about the sourceFile object, read the get-source docs.

Cleaning Output

Synchronously (use with caution in browsers):

stack = stack.clean ()

...or (asynchronously):

stack = await stack.cleanAsync ()

It does the following:

  1. Reads sources (if available)
  2. Excludes locations marked with the isThirdParty flag (library calls)
  3. Excludes locations marked with a // @hide comment (user defined exclusion)
  4. Merges repeated lines (via the .mergeRepeatedLines)

You can customize its behavior by overriding the isClean (entry, index) predicate.

Custom isThirdParty Predicate

You can override the isThirdParty behavior by subclassing StackTracey:

class MyStackTracey extends StackTracey {

    isThirdParty (path, externalDomain) {    // you can use externalDomain to include traces from libs from other domains
        return (super.isThirdParty (path)    // include default behavior
                || path.includes ('my-lib')) // paths including 'my-lib' will be marked as thirdParty
                && !path.includes ('jquery') // jquery paths won't be marked as thirdParty
    }
}

...

const stack = new MyStackTracey (error).withSources ()

Pretty Printing

const prettyPrintedString = new StackTracey (error).withSources ().asTable ()
const prettyPrintedString = (await new StackTracey (error).withSourcesAsync ()).asTable () // asynchronous version

...or (for pretty printing cleaned output):

const prettyPrintedString = new StackTracey (error).clean ().asTable ()
const prettyPrintedString = (await new StackTracey (error).cleanAsync ()).asTable () // asynchronous version

It produces a nice compact table layout (thanks to as-table), supplied with source lines (if available):

at shouldBeVisibleInStackTrace     test.js:25                 const shouldBeVisibleInStackTrace = () => new StackTracey ()
at it                              test.js:100                const stack = shouldBeVisibleInStackTrace ()                
at callFn                          mocha/lib/runnable.js:326  var result = fn.call(ctx);                                  
at run                             mocha/lib/runnable.js:319  callFn(this.fn);                                            
at runTest                         mocha/lib/runner.js:422    test.run(fn);                                               
at                                 mocha/lib/runner.js:528    self.runTest(function(err) {                                
at next                            mocha/lib/runner.js:342    return fn();                                                
at                                 mocha/lib/runner.js:352    next(suites.pop());                                         
at next                            mocha/lib/runner.js:284    return fn();                                                
at <anonymous>                     mocha/lib/runner.js:320    next(0);                  

If you find your pretty printed tables undesirably trimmed (or maybe too long to fit in the line), you can provide custom column widths when calling asTable (...or, alternatively, by overriding maxColumnWidths () method):

stack.asTable ({
    callee:     30,
    file:       60,
    sourceLine: 80
})

Using As A Custom Exception Printer In Node

You can even replace the default NodeJS exception printer with this! This is how you can do it:

process.on ('uncaughtException',  e => { /* print the stack here */ })
process.on ('unhandledRejection', e => { /* print the stack here */ })

But the most simple way to achieve that is to use the ololog library (that is built upon StackTracey and several other handy libraries coded by me). Check it out, it's pretty awesome and will blow your brains out :)

const log = require ('ololog').handleNodeErrors ()

// you can also print Errors by simply passing them to the log() function
screen shot 2018-05-11 at 19 51 03

Parsing SyntaxError instances

For example, when trying to require a file named test_files/syntax_error.js:

// next line contains a syntax error (not a valid JavaScript)
foo->bar ()

...the pretty printed call stack for the error thrown would be something like:

at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()
at it                              test.js:184                   try { require ('./test_files/syntax_error.js') }
at runCallback                     timers.js:781
at tryOnImmediate                  timers.js:743
at processImmediate [as _immediat  timers.js:714

...where the first line is generated from parsing the raw output from the util.inspect call in Node. Unfortunately, this won't work in older versions of Node (v4 and below) as these versions can't provide any meaningful information for a SyntaxError instance.

Array Methods

All StackTracey instances expose map, filter, concat and slice methods. These methods will return mapped, filtered, joined, reversed and sliced StackTracey instances, respectively:

s = new StackTracey ().slice (1).filter (x => !x.thirdParty) // current stack shifted by 1 and cleaned from library calls

s instanceof StackTracey // true

Extra Stuff

You can compare two locations via this predicate (tests file, line and column for equality):

StackTracey.locationsEqual (a, b)

To force-reload the sources, you can invalidate the global source cache:

StackTracey.resetCache ()

Projects That Use StackTracey

  • Ololog — a better console.log for the log-driven debugging junkies!
  • CCXT — a cryptocurrency trading library that supports 130+ exchanges
  • pnpm — a fast, disk space efficient package manager (faster than npm and Yarn!)
  • panic-overlay — a lightweight standalone alternative to react-error-overlay

Current Tags

  • 2.1.8                                ...           latest (3 years ago)

141 Versions

  • 2.1.8                                ...           3 years ago
  • 2.1.7                                ...           4 years ago
  • 2.1.6                                ...           4 years ago
  • 2.1.5                                ...           4 years ago
  • 2.1.4                                ...           4 years ago
  • 2.1.3                                ...           4 years ago
  • 2.1.2                                ...           4 years ago
  • 2.1.1                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.19                                ...           4 years ago
  • 2.0.18                                ...           4 years ago
  • 2.0.17                                ...           4 years ago
  • 2.0.16                                ...           4 years ago
  • 2.0.15                                ...           4 years ago
  • 2.0.14                                ...           4 years ago
  • 2.0.13                                ...           4 years ago
  • 2.0.12                                ...           4 years ago
  • 2.0.11                                ...           4 years ago
  • 2.0.10                                ...           4 years ago
  • 2.0.9                                ...           4 years ago
  • 2.0.8                                ...           4 years ago
  • 2.0.7                                ...           4 years ago
  • 2.0.6                                ...           4 years ago
  • 2.0.5                                ...           4 years ago
  • 2.0.4                                ...           4 years ago
  • 2.0.3                                ...           4 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 1.2.127                                ...           5 years ago
  • 1.2.126                                ...           5 years ago
  • 1.2.125                                ...           5 years ago
  • 1.2.123                                ...           5 years ago
  • 1.2.122                                ...           5 years ago
  • 1.2.121                                ...           5 years ago
  • 1.2.120                                ...           5 years ago
  • 1.2.119                                ...           5 years ago
  • 1.2.117                                ...           5 years ago
  • 1.2.116                                ...           5 years ago
  • 1.2.115                                ...           5 years ago
  • 1.2.112                                ...           6 years ago
  • 1.2.111                                ...           6 years ago
  • 1.2.110                                ...           6 years ago
  • 1.2.109                                ...           6 years ago
  • 1.2.108                                ...           6 years ago
  • 1.2.107                                ...           6 years ago
  • 1.2.106                                ...           6 years ago
  • 1.2.105                                ...           6 years ago
  • 1.2.102                                ...           7 years ago
  • 1.2.101                                ...           7 years ago
  • 1.2.100                                ...           7 years ago
  • 1.2.99                                ...           7 years ago
  • 1.2.98                                ...           7 years ago
  • 1.2.97                                ...           7 years ago
  • 1.2.96                                ...           7 years ago
  • 1.2.95                                ...           7 years ago
  • 1.2.94                                ...           7 years ago
  • 1.2.93                                ...           7 years ago
  • 1.2.92                                ...           7 years ago
  • 1.2.91                                ...           7 years ago
  • 1.2.90                                ...           7 years ago
  • 1.2.89                                ...           7 years ago
  • 1.2.88                                ...           7 years ago
  • 1.2.87                                ...           7 years ago
  • 1.2.86                                ...           7 years ago
  • 1.2.85                                ...           7 years ago
  • 1.2.84                                ...           7 years ago
  • 1.2.83                                ...           7 years ago
  • 1.2.82                                ...           7 years ago
  • 1.2.81                                ...           7 years ago
  • 1.2.80                                ...           7 years ago
  • 1.2.79                                ...           7 years ago
  • 1.2.78                                ...           7 years ago
  • 1.2.77                                ...           7 years ago
  • 1.2.76                                ...           7 years ago
  • 1.2.75                                ...           7 years ago
  • 1.2.74                                ...           7 years ago
  • 1.2.73                                ...           7 years ago
  • 1.2.72                                ...           7 years ago
  • 1.1.72                                ...           7 years ago
  • 1.0.71                                ...           7 years ago
  • 1.0.70                                ...           7 years ago
  • 1.0.69                                ...           7 years ago
  • 1.0.68                                ...           7 years ago
  • 1.0.67                                ...           7 years ago
  • 1.0.66                                ...           7 years ago
  • 1.0.65                                ...           7 years ago
  • 1.0.64                                ...           7 years ago
  • 1.0.63                                ...           7 years ago
  • 1.0.62                                ...           7 years ago
  • 1.0.61                                ...           7 years ago
  • 1.0.60                                ...           7 years ago
  • 1.0.59                                ...           7 years ago
  • 1.0.58                                ...           7 years ago
  • 1.0.57                                ...           7 years ago
  • 1.0.56                                ...           7 years ago
  • 1.0.55                                ...           7 years ago
  • 1.0.54                                ...           7 years ago
  • 1.0.53                                ...           7 years ago
  • 1.0.52                                ...           7 years ago
  • 1.0.51                                ...           7 years ago
  • 1.0.50                                ...           7 years ago
  • 1.0.49                                ...           7 years ago
  • 1.0.48                                ...           7 years ago
  • 1.0.47                                ...           7 years ago
  • 1.0.46                                ...           7 years ago
  • 1.0.45                                ...           7 years ago
  • 1.0.44                                ...           7 years ago
  • 1.0.43                                ...           7 years ago
  • 1.0.42                                ...           7 years ago
  • 1.0.41                                ...           7 years ago
  • 1.0.40                                ...           7 years ago
  • 1.0.39                                ...           7 years ago
  • 1.0.38                                ...           7 years ago
  • 1.0.37                                ...           7 years ago
  • 1.0.36                                ...           8 years ago
  • 1.0.35                                ...           8 years ago
  • 1.0.34                                ...           8 years ago
  • 1.0.33                                ...           8 years ago
  • 1.0.32                                ...           8 years ago
  • 1.0.31                                ...           8 years ago
  • 1.0.30                                ...           8 years ago
  • 1.0.29                                ...           8 years ago
  • 1.0.28                                ...           8 years ago
  • 1.0.27                                ...           8 years ago
  • 1.0.26                                ...           8 years ago
  • 1.0.25                                ...           8 years ago
  • 1.0.24                                ...           8 years ago
  • 1.0.23                                ...           8 years ago
  • 1.0.22                                ...           8 years ago
  • 1.0.21                                ...           8 years ago
  • 1.0.20                                ...           8 years ago
  • 1.0.19                                ...           8 years ago
  • 1.0.18                                ...           8 years ago
  • 1.0.17                                ...           8 years ago
  • 1.0.16                                ...           8 years ago
  • 1.0.15                                ...           8 years ago
  • 1.0.14                                ...           8 years ago
  • 1.0.13                                ...           8 years ago
  • 1.0.12                                ...           8 years ago
  • 1.0.11                                ...           8 years ago
  • 1.0.10                                ...           8 years ago
Maintainers (2)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE