New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-nested-error

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-nested-error

Lightweight xplatform nested error implementation with native TypeScript support.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
408
decreased by-40.52%
Maintainers
1
Weekly downloads
 
Created
Source

ts-nested-error

npm version Build Status Coverage Status

Super lightweight crossplatform (browser compatible) dependency-free nested error implementation.

new NestedError(message: string, public readonly innerError?: Error)

Class that provides readonly innerError?: Error property alongside with an error callstack (as .stack property) of original error eagerly combined with itself's. Deeply nested errors form a list of callstacks and error messages that are concatenated.

Example:

    try {
        throw new Error('Connection timed out');
    } catch (err) {
        throw new NestedError(`Oh no, couldn't load file! More info in inner error`, err);
    }

This code will produce an error that when stringifed shows the following message:

NestedError: Oh no, couldn't load file! More info in inner error
    at someMethod (/path/to/code.js:line:column)
    at ...

======= INNER ERROR =======

Error: Connection timed out
    at someMethod (/path/to/code.js:line:column)
    at ...

NestedError.rethrow(message: string)

Returns a function that throws NestedError or an object of class that is derived from it with the given message. This is mostly intended to be a shorthand to create error wrapping callbacks for Promises:

import { NestedError } from 'ts-nested-error';

class DbError extends NestedError {}

database.get().then(
    data => console.log(`Hooray! data: ${data}`),
    DbError.rethrow('some database error happened') // throws instanceof DbError
);

toError(err)

Returns err itself if err instanceof Error === true, otherwise attemts to stringify it and wrap into Error object to be returned.

const err = new Error('oops');

// noop if err instanceof Error
toError(err) === err; 

// wrapped 42 into Error with warning message
(toError(42) instanceof Error) === true; 

toError('non-error value').message === `Value that is not an instance of Error was thrown: non-error value`

Keywords

FAQs

Package last updated on 20 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc