Detalhes do pacote

@visulima/error

visulima2.2kMIT4.4.16

Error with more than just a message, stacktrace parsing.

anolilab, character, code-frame, codeframe

readme (leia-me)

Visulima error

Error with more than just a message, stacktrace parsing and sourcemap loading.


typescript-image npm-image license-image

Daniel Bannert's open source work is supported by the community on GitHub Sponsors


Install

npm install @visulima/error
yarn add @visulima/error
pnpm add @visulima/error

Usage

Extend the VisulimaError

import { VisulimaError } from "@visulima/error";

class MyError extends VisulimaError {
    constructor(message: string) {
        super({
            name: "MyError",
            message,
        });
    }
}

throw new MyError("My error message");

// or

const error = new MyError("My error message");

error.hint = "My error hint";

throw error;

Get all causes in a error

import { getErrorCauses } from "@visulima/error";

const error = new Error("My error message");
const error2 = new Error("Nested Error");

error.cause = error2;

// The getErrorCauses function will return an array of all causes in the error in the order they occurred.
const causes = getErrorCauses(error);

console.log(causes);

// [
//     {
//         message: "My error message",
//         name: "Error",
//         stack: "Error: My error message\n    at Object.<anonymous> (/visulima/packages/error/src/index.ts:2:16)",
//     },
//     {
//         message: "Nested Error",
//         name: "Error",
//         stack: "Error: Nested Error\n    at Object.<anonymous> (/visulima/packages/error/src/index.ts:3:16)",
//     },
// ];

Pretty Code Frame

Display a pretty code frame with the error location.

Note: Tabs can be used in the source code, codeFrame transforms them to spaces based on the tabWidth option. The default tabWidth is 4, to disable the transformation, set tabWidth to false.

import { codeFrame } from "@visulima/error";

const source = "const x = 10;\nconst error = x.y;\n";
const loc = { column: 16, line: 2 };

const frame = codeFrame(source, { start: loc });

console.log(frame);
//   1 | const x = 10;
// > 2 | const error = x.y;
//     |                ^

API

source

Type: string

The source code to frame.

location

Type: object

The location of the error.

location.start

Type: object

The location of the start of the frame.

location.start.line

Type: number

The line number of the error.

location.start.column

Type: number

The column number of the error.

location.end

Type: object

The location of the end of the frame.

location.end.line

Type: number

The line number of the error.

location.end.column

Type: number

The column number of the error.

options

Type: object

options.linesAbove

Type: number

Default: 2

The number of lines to show above the error.

options.linesBelow

Type: number

Default: 3

The number of lines to show below the error.

options.tabWidth

Type: number | false

Default: 4

Stacktrace

Browser older than 6 years are not supported.

Currently supported browsers/platforms:

  • Firefox
  • Chrome
  • Webkit / Safari
  • Edge
  • Node / Node V8
  • Opera (Chromium based)
import { parseStacktrace } from "@visulima/error";

const error = new Error("My error message");

const stack = parseStacktrace(error);

console.log(stack);

// [
//     {
//         column: 16,
//         file: "file:///Users/danielbannert/Projects/visulima/packages/error/src/index.ts",
//         line: 2,
//         methodName: "Object.<anonymous>",
//         raw: "    at Object.<anonymous> (/visulima/packages/error/src/index.ts:2:16)",
//         type: undefined, // optional property, can be undefined, "eval", "native", or "internal"
//         evalOrigin: undefined, // optional property only available if the stacktrace contains eval
//     },
//     ...and so on
// ];

API

error

Type: Error

The error to parse.

options

Type: object

options.filter

Type: Function

A function to filter the stack frames.

options.frameLimit

Type: number

The maximum number of frames to parse.

serialize an error object

  • Ensures errors are safe to serialize with JSON
  • Can be used as error.toJSON()
  • Deep serialization, including transforming
  • Custom serialization (e.g. YAML or process.send())
  • Keeps both native (TypeError, etc.) and custom error classes
  • Preserves errors' additional properties
  • Can keep constructor's arguments
  • Works recursively with error.cause and AggregateError
  • Buffer properties are replaced with [object Buffer]
  • Circular references are handled.
  • If the input object has a .toJSON() method, then it's called instead of serializing the object's properties.
  • It's up to .toJSON() implementation to handle circular references and enumerability of the properties.
import { serializeError } from "@visulima/error";

const error = new TypeError("example");
const errorObject = serializeError(error);
// Plain object: { name: 'TypeError', message: 'example', stack: '...' }

const errorString = JSON.stringify(errorObject);
const newErrorObject = JSON.parse(errorString);

renderError - pretty print an error

import { renderError } from "@visulima/error";

const error = new Error("This is an error message");

console.log(renderError(error));

// Error: This is an error message
//
// at <unknown> file:///home/visulima/visulima/examples/error/node/render-error.js:5
//   1 | import { renderError } from "@visulima/error";
//   2 |
//   3 | const error = new Error("This is an error message");
//   4 |
// ❯ 5 | console.log(renderError(new Error("This is an error message")));
//     |                         ^
//   6 |
//
// at ModuleJob.run node:internal/modules/esm/module_job:195
// at async ModuleLoader.import node:internal/modules/esm/loader:336
// at async loadESM node:internal/process/esm_loader:34
// at async handleMainPromise node:internal/modules/run_main:106

colorized output

Use the @visulima/colorize, chalk or some other package to colorize the output.

colorized output

API

error

Type: AggregateError | Error | VisulimaError \ The error to render.

options

Type: object

options.color

Type: object \ The color options.

options.cwd

Type: string

The current working directory.

options.displayShortPath

Type: boolean \ Default: false

Display the short path.

options.framesMaxLimit

Type: number \ Default: Number.Infinity

The maximum number of frames to display.

options.hideErrorCauseCodeView

Type: boolean \ Default: false

Hide the error cause code view.

options.hideErrorCodeView

Type: boolean \ Default: false

Hide the error code view.

options.hideErrorTitle

Type: boolean \ Default: false

Hide the error title.

options.hideMessage

Type: boolean \ Default: false

Hide the error message.

captureRawStackTrace

Capture a raw stack trace.

import { captureRawStackTrace } from "@visulima/error";

const stack = captureRawStackTrace();

console.log(stack);

Supported Node.js Versions

Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

About

Related Projects

License

The visulima error is open-sourced software licensed under the MIT

changelog (log de mudanças)

@visulima/error 4.4.16 (2025-03-07)

Bug Fixes

  • updated @visulima/packem and other dev deps, for better bundling size (e940581)

Miscellaneous Chores

  • updated dev dependencies (487a976)

Dependencies

  • @visulima/path: upgraded to 1.3.5

@visulima/error 4.4.15 (2025-01-25)

Bug Fixes

  • fixed wrong node version range in package.json (4ae2929)

Miscellaneous Chores

  • fixed typescript url (fe65a8c)
  • updated all dev dependencies (37fb298)

Dependencies

  • @visulima/path: upgraded to 1.3.4

@visulima/error 4.4.14 (2025-01-22)

Bug Fixes

  • error: dont display a main frame if the error has a broken state data, updated dev deps (c0b8f15)

Miscellaneous Chores

  • error: fixed new test value (cacee8b)
  • updated all dev dependencies and all dependencies in the app folder (87f4ccb)

@visulima/error 4.4.13 (2025-01-13)

Dependencies

  • @visulima/path: upgraded to 1.3.3

@visulima/error 4.4.12 (2025-01-12)

Bug Fixes

  • updated @visulima/packem, and all other dev dependencies (7797a1c)

Dependencies

  • @visulima/path: upgraded to 1.3.2

@visulima/error 4.4.11 (2025-01-08)

Dependencies

  • @visulima/path: upgraded to 1.3.1

@visulima/error 4.4.10 (2025-01-08)

Dependencies

  • @visulima/path: upgraded to 1.3.0

@visulima/error 4.4.9 (2024-12-31)

Miscellaneous Chores

  • updated dev dependencies (9de2eab)

Dependencies

  • @visulima/path: upgraded to 1.2.0

@visulima/error 4.4.8 (2024-12-12)

Bug Fixes

  • allow node v23 (8ca929a)
  • allowed node 23, updated dev dependencies (f99d34e)
  • error: fixed path separator to always be / (2cac82f)
  • error: fixed path separator to always be / (d9f5d26)
  • updated packem to v1.8.2 (23f869b)
  • updated packem to v1.9.2 (47bdc2d)

Styles

Miscellaneous Chores

  • updated dev dependencies (a916944)

Dependencies

  • @visulima/path: upgraded to 1.1.2

@visulima/error 4.4.7 (2024-10-05)

Dependencies

  • @visulima/path: upgraded to 1.1.1

@visulima/error 4.4.6 (2024-10-05)

Bug Fixes

  • updated dev dependencies, updated packem to v1.0.7, fixed naming of some lint config files (c071a9c)

Dependencies

  • @visulima/path: upgraded to 1.1.0

@visulima/error 4.4.5 (2024-09-24)

Bug Fixes

  • update packem to v1 (05f3bc9)
  • updated esbuild from v0.23 to v0.24 (3793010)

Miscellaneous Chores

  • updated dev dependencies (05edb67)

Dependencies

  • @visulima/path: upgraded to 1.0.9

@visulima/error 4.4.4 (2024-09-11)

Bug Fixes

Miscellaneous Chores

  • updated dev dependencies (28b5ee5)

Dependencies

  • @visulima/path: upgraded to 1.0.8

@visulima/error 4.4.3 (2024-09-07)

Bug Fixes

  • fixed broken chunk splitting from packem (1aaf277)

Dependencies

  • @visulima/path: upgraded to 1.0.7

@visulima/error 4.4.2 (2024-09-07)

Bug Fixes

  • added types support for node10 (604583f)

Styles

Miscellaneous Chores

  • update dev dependencies (0738f98)

Dependencies

  • @visulima/path: upgraded to 1.0.6

@visulima/error 4.4.1 (2024-08-30)

Miscellaneous Chores

  • updated dev dependencies (45c2a76)

Dependencies

  • @visulima/path: upgraded to 1.0.5

@visulima/error 4.4.0 (2024-08-08)

Features

  • error: added captureRawStackTrace (1c988a3)

Tests

  • error: fixed assertions on test (f315754)

@visulima/error 4.3.2 (2024-08-04)

Dependencies

  • @visulima/path: upgraded to 1.0.4

@visulima/error 4.3.1 (2024-08-01)

Styles

Miscellaneous Chores

  • added private true into fixture package.json files (4a9494c)
  • updated dev dependencies (ac67ec1)
  • updated dev dependencies and sorted the package.json (9571572)

Dependencies

  • @visulima/path: upgraded to 1.0.3

@visulima/error 4.3.0 (2024-07-02)

Features

  • error: added a prefix option to renderError (e164d52)
  • error: added stackFilter to parseStacktrace, fixed parsing AggregateError stack, added filterStacktrace to renderError (bb4b4f7)

Bug Fixes

  • error: renamed stackFilter to filter and changed interface for filterStacktrace (b95dfb6)

Miscellaneous Chores

  • changed typescript version back to 5.4.5 (55d28bb)

@visulima/error 4.2.0 (2024-07-02)

Features

  • error: added renderError, added cause to visulima-error (#449) (4e78638)

Miscellaneous Chores

  • updated dev dependencies (34df456)

@visulima/error 4.1.0 (2024-07-01)

Features

Miscellaneous Chores

  • updated dev dependencies (c889486)

@visulima/error 4.0.0 (2024-06-16)

⚠ BREAKING CHANGES

  • error: moved source-map handling into a new package @visulima/source-map Signed-off-by: prisis d.bannert@anolilab.de

Signed-off-by: prisis d.bannert@anolilab.de

Features

  • error: removed source-map handling (716ef11)
  • source-map: new source-map package (d4114c6)

Bug Fixes

  • error: fixed new eval syntax on windows (e50a816)

Miscellaneous Chores

  • error: added missing @visulima/path as dev dep (c83d103)
  • source-map: moved fixtures from error to source-map (664b287)
  • updated all dev deps (ef143ce)

Build System

  • fixed found audit error, updated all dev package deps, updated deps in apps and examples (4c51950)

@visulima/error 3.2.11 (2024-06-06)

Bug Fixes

Dependencies

  • @visulima/path: upgraded to 1.0.2
  • @visulima/nextra-theme-docs: upgraded to 4.0.26

@visulima/error 3.2.10 (2024-06-05)

Miscellaneous Chores

  • updated dev dependencies (a2e0504)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.25

@visulima/error 3.2.9 (2024-05-24)

Bug Fixes

Miscellaneous Chores

  • changed semantic-release-npm to pnpm (b6d100a)

Dependencies

  • @visulima/path: upgraded to 1.0.1

@visulima/error 3.2.8 (2024-05-15)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.24

@visulima/error 3.2.7 (2024-04-27)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.23

@visulima/error 3.2.6 (2024-04-17)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.22

@visulima/error 3.2.5 (2024-04-09)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.21

@visulima/error 3.2.4 (2024-03-30)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.20

@visulima/error 3.2.3 (2024-03-27)

Bug Fixes

  • added missing os key to package.json (4ad1268)

@visulima/error 3.2.2 (2024-03-22)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.19

@visulima/error 3.2.1 (2024-03-16)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.18

@visulima/error 3.2.0 (2024-03-11)

Features

  • added new index-to-line-column function (c794be0)

@visulima/error 3.1.2 (2024-03-10)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.17

@visulima/error 3.1.1 (2024-03-09)

Bug Fixes

  • error: fixed color option override (467f1dc)

@visulima/error 3.1.0 (2024-03-09)

Features

  • error: added new tabWidth option to disable the tab transformer (a115918)

@visulima/error 3.0.7 (2024-03-09)

Bug Fixes

  • error: added missing CodeFrameLocation export (d4144e0)

@visulima/error 3.0.6 (2024-03-06)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.16

@visulima/error 3.0.5 (2024-03-04)

Bug Fixes

  • fixed all found type issues (eaa40d1)
  • minifyWhitespace on prod build, removed @tsconfig/* configs (410cb73)
  • updated @jridgewell/trace-mapping (2955e4a)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.15

@visulima/error 3.0.4 (2024-02-26)

Bug Fixes

  • updated @jridgewell/trace-mapping (f2061d1)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.14

@visulima/error 3.0.3 (2024-01-31)

Bug Fixes

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.13

@visulima/error 3.0.2 (2024-01-19)

Bug Fixes

  • updated all deps, updated test based on eslint errors (909f8f3)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.12

@visulima/error 3.0.1 (2023-12-06)

Bug Fixes

  • fixed possible deep nesting of error cause in getErrorCauses (350ddba)

@visulima/error 3.0.0 (2023-12-06)

⚠ BREAKING CHANGES

  • removed ErrorWithMetadata, fixed wrong Trace on error namespace Signed-off-by: prisis d.bannert@anolilab.de

Features

  • added new getErrorCauses helper (e94d9ee)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.11

@visulima/error 2.0.0 (2023-12-04)

⚠ BREAKING CHANGES

Features

  • changed public interface of code-frame, added multiline marker (d9d60c0)

Bug Fixes

@visulima/error 1.1.1 (2023-11-30)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.10

@visulima/error 1.1.0 (2023-11-30)

Features

  • added a stacktrace parser and a sourcemap loader (#250) (4543e44)

@visulima/error 1.0.2 (2023-11-30)

Bug Fixes

  • deps: updated package deps (b4f4ede)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.9

@visulima/error 1.0.1 (2023-11-07)

Bug Fixes

  • fixed the homepage url of the package (02075ce)

Dependencies

  • @visulima/nextra-theme-docs: upgraded to 4.0.8

@visulima/error 1.0.0 (2023-11-07)

Features