typed-error
TypeScript icon, indicating that this package has built-in type declarations

3.2.2 • Public • Published

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

For typescript:

import { TypedError } from '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 } from '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')

Readme

Keywords

none

Package Sidebar

Install

npm i typed-error

Weekly Downloads

76,776

Version

3.2.2

License

Apache-2.0

Unpacked Size

35.3 kB

Total Files

20

Last publish

Collaborators

  • page
  • balena.io