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

checked-exceptions

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

checked-exceptions

A utility library to create and manage checked exceptions in typescript

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

checked-exceptions

Build Status npm

A utility library to create and manage checked exceptions in typescript.

Index

Installation

npm:

npm i checked-exceptions --save

yarn:

yarn add checked-exceptions

Usage

Create

A checked exception can be created by just importing check function for the library and passing it the type of the exception.

import {check} from 'checked-exceptions'

const NotImplemented = check('NotImplemented')

throw new NotImplemented()

Instantiate

Once a checked exception class has been created, an instance of the checked exception can be created using the new operator or using the static of() function, for eg:

Using the new operator

const err = new NotImplemented()

Using the of function

const err = NotImplemented.of()

Customize

A custom exception with additional meta data can be created by passing a second argument for eg:

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

throw new UserIdNotFound({id: 1900, name: 'Foo'})

The second argument is of type function and is used to generate the message string when the exception is thrown.

Access Data

The default properties of an exception such as stack and message work like they do in a typical Error object. Additional properties such as data , type is also added, for eg:

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

const err = new UserIdNotFound({id: 1900, name: 'Foo'})

console.log(err.data.id) // prints 1900
console.log(err.type) // prints 'UserIdNotFound'

Access Type

To access the type of the exception you can use the info property on the created checked exception —

type User = {id: number; name: string}

const UserIdNotFound = check(
  'UserIdNotFound',
  (user: User) => `Could not find user with id: ${user.id}`
)

type UserIdNodeFoundException = typeof UserIdNotFound.info

const err: UserIdNodeFoundException = new UserIdNotFound({
  id: 1900,
  name: 'Foo'
})

Keywords

FAQs

Package last updated on 21 Jul 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