Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typed-error

balena-io-modules914.3kApache-2.03.2.2TypeScript support: included

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

readme

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')

changelog

Change Log

All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to Semantic Versioning.

3.2.2 - 2023-04-20

  • Delete CODEOWNERS [dfunckt]
  • Update dependencies [Thodoris Greasidis]
  • Replace circleCI with flowzone [Thodoris Greasidis]

v3.2.1

(2020-08-05)

  • Update dependencies [Pagan Gazzard]

3.2.0 - 2019-11-20

  • Update deps and specify minimum engine requirements [Will Boyce]

3.1.0 - 2019-04-01

  • Dev: Enforce prettier coding standards [Will Boyce]
  • Npm: Update dependencies and remove package-lock.json [Will Boyce]
  • Codeowners: Add top contributors @wrboyce, @Page-, and @dfunckt [Will Boyce]
  • Versionbot: Add CHANGELOG.yml (for nested changelogs) [Will Boyce]

v3.0.2 - 2018-11-01

  • Update README with new import style [CameronDiver]

v3.0.1 - 2018-10-29

  • Update dev dependencies [Pagan Gazzard]
  • Update to typescript 3 [Pagan Gazzard]

v3.0.0 - 2018-04-17

  • Update dependencies, clean up package/tsconfig #12 [Will Boyce]
  • Remove BaseError class and directly subclass Error #12 [Will Boyce]
  • Use circle for build/publish and add package-lock #12 [Will Boyce]
  • Add lint scripts/requirements #12 [Will Boyce]
  • Distribute generated typescript declaration #12 [Will Boyce]

v2.0.1 - 2017-12-15

  • Add LICENSE #11 [Akis Kesoglou]

v2.0.0 - 2017-07-25

  • Drop UMD support, produce CommonJS module [Eugene Mirotin]

v1.1.1 - 2017-07-25

  • Revert tslib import [Eugene Mirotin]

v1.1.0 - 2017-07-21

  • Fix build [Eugene Mirotin]
  • Add .editorconfig and reformat couple of files [Eugene Mirotin]
  • Fix tests [Eugene Mirotin]

v1.0.1

  • Ensure the name property in the Function interface is readonly. [Heds Simons]

v1.0.0

  • Avoid creating an error object for the stack if Error.captureStackTrace is available and a string message is passed.
  • Switch to typescript for the main code
  • Fix running tests