parsetrace

Minimal library for parsing and printing node stacktrace. Mostly for use in express-error-with-sources.

Usage no npm install needed!

<script type="module">
  import parsetrace from 'https://cdn.skypack.dev/parsetrace';
</script>

README

node-parsetrace NPM version Build Status Dependency Status

Minimal library for parsing and printing node stacktrace. Mostly for use in express-error-with-sources.

Installation

npm install --save parsetrace

Example

var parsetrace = require('parsetrace');

try {
    throw new Error('My cool error');
} catch (e) {
    console.log(parsetrace(e, { sources: true }).json());
}

API

parsetrace(error, [options], [callback])

Parses stack trace from Error, string or object, that have .stack and .message property.

If callback is passed - all source code fetching will be async and error will be passed to it (even if strict is true).

parsetrace(err, function(error, trace) {
    console.log(trace.toString());
});

Options:

  • sources - fetch source code from files, that are mentioned in stacktrace. If file can not be found or readed - sources will be fetched silently (unless you enabled strict option) (default: false)
  • strict - throws errors, while parsing stacktrace and fetching source code (default: false)
  • contextSize - number of lines before and after the error line (default: 3)

Returns Object with methods described below:

json()

{
    "error": "My cool error",
    "frames": [
        {
            "line": 4,
            "column": 11,
            "function": "Object.<anonymous>",
            "file": "/full/path/to/file.js",
            "source": {
                "2": { "code": "" },
                "3": { "code": "try {" },
                "4": { "code": "    throw new Error('My cool error');" },
                "5": { "code": "} catch (e) {" },
                "6": { "code": "    console.log(parsetrace(e).json());" }
            }
        }
    ]
}

Returns stacktrace as Json string

object()

Format of object you can see in json() section.

Returns stacktrace as Object

toString([options])

Options:

  • excludeSources - if stacktrace was parsed with sources, this will exclude them from output (default: false)

Returns stacktrace as NodeJS formatted stacktracke string.