npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@selfage/nested_error

v1.0.7

Published

Builds nested errors while preserving stacks.

Downloads

22

Readme

@selfage/nested_error

Install

npm install @selfage/nested_error

Overview

Written in TypeScript and compiled to ES6 with inline source map & source. See @selfage/tsconfig for full compiler options. Provides a simple implementation to take an error as the cause and output nested stacks with "Caused by: ".

Constructor

Supply an existing error as the cause like the following.

import { StdError } from '@selfage/nested_error';

throw new StdError('more context', new Error('Failure'));

It will output the following, omitting some details.

StdError: more context
    at Object.<anonymous> (.../nested_error/temp.js:4:7)
    ...
Caused by: Error: Failure
    at Object.<anonymous> (.../nested_error/temp.js:4:47)
    ...

And of course, it's optional to supply an existing error.

Any cause

When catching an error, it's often of any type, which can be passed to StdError without type assertion/casting, i.e., when stack property is not available, it calls toString() to generate the output.

import { StdError } from '@selfage/nested_error';

try {
  throw 'Failed!';
} catch (e) {
  throw new StdError('some issue', e);
}

It will output the following, omitting details.

StdError: some issue
    at Object.<anonymous> (.../nested_error/temp.js:8:11)
    ...
Caused by: Failed!

Subclass

To define subclasses, you should extend NestedError instead.

import { NestedError } from '@selfage/nested_error';

class CustomError extends NestedError {
  public constructor(message?: string, cause?: any) {
    super('CustomError', message, cause);
  }
}

throw new CustomError('more context', new Error('Failure'));
CustomError: more context
    at Object.<anonymous> (.../nested_error/temp.js:9:7)
    ...
Caused by: Error: Failure
    at Object.<anonymous> (.../nested_error/temp.js:9:39)
    ...

Note that you need to pass the class name to super() because of minification.

Minification

StdError is the simplest subclass of NestedError. All subclasses of NestedError require their names to be explicitly passed into super(), such that even if class names are minified/mangled, stacks still show the right error names.

However, the downside is that subclasses of StdError cannot change their error names.

Compatibility

With ES6, we can simply extend native Error class, without the need to fix issues such as broken prototype chain.

new StdError('failure') instanceof Error; // true

However, it could bring compatibility issue to old browsers.

Stack trace from TypeScript source file

If you are developing in TypeScript, printing stack trace from TypeScript source file is often desired. One option is to install source-map-support.

import { StdError } from '@selfage/nested_error';
import 'source-map-support/register';

throw new StdError('more context', new Error('Failure'));

It will output the following, omitting details.

StdError: more context
    at Object.<anonymous> (.../nested_error/temp.ts:4:7)
    ...
Caused by: Error: Failure
    at Object.<anonymous> (.../nested_error/temp.ts:4:39)
    ...