Socket
Socket
Sign inDemoInstall

@forward-steady/exception

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @forward-steady/exception

An Exception base class for Javascript/Typescript that extends the built-in Error class with code and context properties.


Version published
Maintainers
1
Created

Readme

Source

Exception

An Exception base class for Javascript/Typescript that extends the built-in Error class with code and context properties.

Install

For npm users:

npm install @forward-steady/exception

for yarn users:

yarn add @forward-steady/exception

Usage

The Exception class derives from the Error class and can be use in a similiar manner. It includes optional code and context properties that can be provided in the constructor.

const message = 'Unable to perform action X';
const code = 'action-x-failed';
const context = {
  recordId: 5003
}

throw new Exception(message, code, context);

Since the Exception class derives from the Error class, it will have a stack property just as an Error instance would.

const exception = new Exception(message, code, context);
console.log(exception.stack)

It is also easy to convert an Error instance into an Exception instance and preserve the message and stack trace of the error. This allows a code and context to be include with the error message and stack trace.

const error = new Error(message)
const exception = Exception.fromError(error, code, context);

expect(exception.message).toBe(error.message);
expect(exception.stack).toBe(error.stack);

expect(exception.code).toBe(code);
expect(exception.context).toBe(context);

Keywords

FAQs

Last updated on 11 Mar 2020

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc