fynjs API
    Preparing search index...

    Module @jchip/error

    @jchip/error

    License build

    Downloads

    npm badge

    Utilities for readable node.js error stacks, and an AggregateError subclass that prints nested stacks.

    Table of Contents

    import { cleanErrorStack } from "@jchip/error";

    function loadConfig() {
    return JSON.parse("{ bad json");
    }

    try {
    loadConfig();
    } catch (err) {
    console.log(cleanErrorStack(err));
    }

    Output, run as node test/app.mjs from /home/me/app:

    SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)
    at loadConfig (test/app.mjs:4:15)
    at test/app.mjs:8:3

    vs err.stack:

    SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)
    at JSON.parse (<anonymous>)
    at loadConfig (file:///home/me/app/test/app.mjs:4:15)
    at file:///home/me/app/test/app.mjs:8:3
    at ModuleJob.run (node:internal/modules/esm/module_job:561:25)
    at async node:internal/modules/esm/loader:647:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

    Frames from node: internals are dropped. Paths under the current directory become relative, for both ES module file:// URLs and CommonJS paths.

    Generate stack with aggregate errors from AggregateError

    Example:

    import { aggregateErrorStack, AggregateError } from "@jchip/error";

    console.log(aggregateErrorStack(new AggregateError([new Error("error 1")], "test")));

    Output:

    AggregateError: test
    at file:///home/me/app/test/app.mjs:3:33
    at ModuleJob.run (node:internal/modules/esm/module_job:561:25)
    at async node:internal/modules/esm/loader:647:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)
    Error: error 1
    at file:///home/me/app/test/app.mjs:3:53
    at ModuleJob.run (node:internal/modules/esm/module_job:561:25)
    at async node:internal/modules/esm/loader:647:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)
    • Subclass of the built-in AggregateError; it does not patch the global
    • stack includes the stacks of the aggregated errors, indented
    • Takes any iterable of errors and the native { cause } option

    Example with cleanErrorStack:

    import { cleanErrorStack, AggregateError } from "@jchip/error";

    function loadConfig() {
    return JSON.parse("{ bad json");
    }

    try {
    loadConfig();
    } catch (err) {
    console.log(cleanErrorStack(new AggregateError([err], "config failed")));
    }

    Output:

    AggregateError: config failed
    at test/app.mjs:10:31
    SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)
    at loadConfig (test/app.mjs:4:15)
    at test/app.mjs:8:3

    vs new AggregateError([err], "config failed").stack:

    AggregateError: config failed
    at file:///home/me/app/test/app.mjs:10:15
    at ModuleJob.run (node:internal/modules/esm/module_job:561:25)
    at async node:internal/modules/esm/loader:647:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)
    SyntaxError: Expected property name or '}' in JSON at position 2 (line 1 column 3)
    at JSON.parse (<anonymous>)
    at loadConfig (file:///home/me/app/test/app.mjs:4:15)
    at file:///home/me/app/test/app.mjs:8:3
    at ModuleJob.run (node:internal/modules/esm/module_job:561:25)
    at async node:internal/modules/esm/loader:647:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)
    AggregateError
    aggregateErrorStack
    aggregateStack
    cleanErrorStack