Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typed-error

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-error

A class designed to enable easily extending the built-in javascript Error, allowing typed errors.

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
138K
increased by7.92%
Maintainers
2
Weekly downloads
 
Created
Source

This module allows you to easily extend the built-in Error type for typed error checking

For typescript:

import TypedError = require('typed-error')

class MyError extends TypedError {}

try {
	throw new MyError()
} catch(e) {
	console.log(e instanceof MyError) // true
	console.log(e.name) // 'MyError'
	console.log(e.constructor.name) // 'MyError'
	console.log(e.stack) // <stack trace>

	if(e instanceof MyError) {
		console.log('Do custom handling')
	} else {
		console.log('Another type of error')
	}

	// Or
	switch(e.name) {
		case 'MyError':
			console.log('Do custom handling')
		break;
		default:
			console.log('Another type of error')
	}

	// Or
	switch(e.constructor.name) {
		case 'MyError':
			console.log('Do custom handling')
		break;
		default:
			console.log('Another type of error')
	}
}

And with bluebird:

import TypedError = require('../src/typed-error')
import * as Promise from 'bluebird'

class MyError extends TypedError {}

Promise.try(() => {
	throw new MyError()
})
.catch(MyError, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})

// Or
const MyErrorName = (e: Error) => e.name === 'MyError'
Promise.try(() => {
	throw new MyError()
})
.catch(MyErrorName, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})

// Or
const MyErrorConstructorName = (e: Error) => e.constructor.name === 'MyError'
Promise.try(() => {
	throw new MyError()
})
.catch(MyErrorConstructorName, (e) => {
	console.log('Do custom handling')
})
.catch(() => {
	console.log('Another type of error')
})

For coffeescript:

TypedError = require 'typed-error'

class MyError extends TypedError

try
	throw new MyError()
catch e
	console.log(e instanceof MyError) # true
	console.log(e.name) # 'MyError'
	console.log(e.constructor.name) # 'MyError'
	console.log(e.stack) # <stack trace>

	if e instanceof MyError
		console.log('Do custom handling')
	else
		console.log('Another type of error')

	# Or
	switch e.name
		when 'MyError'
			console.log('Do custom handling')
		else
			console.log('Another type of error')

	# Or
	switch e.constructor.name
		when 'MyError'
			console.log('Do custom handling')
		else
			console.log('Another type of error')

And with bluebird:

Promise = require 'bluebird'
TypedError = require 'typed-error'

class MyError extends TypedError

Promise.try ->
	throw new MyError()
.catch MyError, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')

# Or
MyErrorName = (e) -> e.name is 'MyError'
Promise.try ->
	throw new MyError()
.catch MyErrorName, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')

# Or
MyErrorConstructorName = (e) -> e.constructor.name is 'MyError'
Promise.try ->
	throw new MyError()
.catch MyErrorConstructorName, (e) ->
	console.log('Do custom handling')
.catch ->
	console.log('Another type of error')

FAQs

Package last updated on 25 Jul 2017

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