JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32
  • Score
    100M100P100Q80085F
  • License MIT

Handle errors with ease

Package Exports

  • @alessiofrittoli/exception
  • @alessiofrittoli/exception/abort
  • @alessiofrittoli/exception/code

Readme

Exception 🚦

NPM Latest Version Coverage Status Socket Status NPM Monthly Downloads Dependencies

GitHub Sponsor

Handle errors with ease

This documentation describes the Exception class, which provides a structured way to handle errors in TypeScript. It includes custom properties such as code, name, and status for more detailed error reporting and debugging.

Table of Contents


Overview

The Exception class extends the native JavaScript Error object, adding customizable fields to improve error classification and handling. It also includes a static utility method to check if an object is an instance of the Exception class.

Getting started

Run the following command to start using @alessiofrittoli/exception in your projects:

npm i @alessiofrittoli/exception

or using pnpm

pnpm i @alessiofrittoli/exception

API Reference

ExceptionOptions Interface

The ExceptionOptions interface defines the optional parameters for the Exception class constructor.

Properties
Property Type Default Description
code TCode - (Required) A numeric or custom code representing the error type.
name string Exception (Optional) A string representing the error name.
status number - (Optional) An HTTP status code associated with the error.

Exception Class

The Exception class extends the Error object and implements the ExceptionOptions interface.

Constructor

The constructor initializes an Exception instance with a custom message and options.

Parameters
Parameter Type Description
message TMessage (Required) The error message. Can be a string or a custom type.
options ExceptionOptions (Required) An object containing the error options.
Example
import { Exception } from '@alessiofrittoli/exception'

try {
  throw new Exception( 'Resource not found', {
    code    : 'ERR:NOTFOUND',
    status  : 404,
  } )
} catch ( error ) {
  console.error( error )
}
Static Methods
isException

A utility method to check if an object is an instance of the Exception class.

It supports also a JSON representation of the Exception class (commonly returned by server responses).

Example
try {
  throw new Exception( 'Something went wrong', { code: 'ERR:UNKNOWN' } )
} catch ( error ) {
  if ( Exception.isException( error ) ) {
    // we can safely access `Exception` properties
    console.error( `Error [${ error.code }]: ${ error.message }` )
  } else {
    console.error( error )
  }
}
/** Simulates JSON Exception returned by a server JSON Response. */
const error = (
  JSON.parse( JSON.stringify( new Exception( 'Exception with custom name.', {
    code: 0,
    name: 'AbortError',
  } ) ) )
)

console.log( Exception.isException( error ) ) // Outputs: true

AbortError Class

The AbortError class extends the Exception Class and offers a preset configuration for easier usage.

Constructor

The constructor initializes an AbortError instance with a custom message and options.

Parameters
Parameter Type Description
message TMessage (Required) The error message. Can be a string or a custom type.
options AbortErrorOptions (Optional) An object containing the error options. It extends the ExceptionOptions Interface and omit the name property. The code defaults to ErrorCode.ABORT but can be optionally customized.

AbortError Usage Scenarios
Precise Abort Errors Handling
import { AbortError } from '@alessiofrittoli/exception/abort'

try {
  const controller = new AbortController()
  const { signal } = controller

  button.addEventListener( 'click', () => {
    controller.abort( new AbortError( 'User cancelled the request.' ) )
  } )

  await fetch( ..., { signal } )
} catch ( error ) {

  if ( AbortError.isException( error ) ) {
    if ( AbortError.isAbortError( error ) ) {
      // handle abort error
      console.log( error.code ) // `error.code` is type of `ErrorCode.ABORT`.
      return
    }
    // handle other Exception errors
    return
  }
  // handle other unknown errors
  
}

Using custom Abort Error codes
import { AbortError } from '@alessiofrittoli/exception/abort'

enum CustomAbortCode
{
  CUSTOM_CODE = 'ERR:CUSTOM_ABORT_CODE',
}

try {
  const controller = new AbortController()
  const { signal } = controller

  button.addEventListener( 'click', () => {
    controller.abort(
      new AbortError( 'User cancelled the request.', { code: CustomAbortCode.CUSTOM_CODE } )
    )
  } )

  await fetch( ..., { signal } )
} catch ( error ) {

  if ( AbortError.isException( error ) ) {
    if ( AbortError.isAbortError( error, CustomAbortCode.CUSTOM_CODE ) ) {
      // handle abort error
      console.log( error.code ) // `error.code` is type of `CustomAbortCode.CUSTOM_CODE`.
      return
    }
    // handle other Exception errors
    return
  }
  // handle other unknown errors
  
}

ErrorCode enum

The ErrorCode enum is a utility that provides pre-defined constants for the most common error codes.
It is designed to simplify error handling and improve feedback quality returned to the user.

Constants Overview
Constant Value Description
UNKNOWN ERR:UNKNOWN Returned when an unexpected error occured.
ABORT ERR:ABORT Returned when user abort the request.
EMPTY_VALUE ERR:EMPTYVALUE Returned when a required value is "falsy".
WRONG_VALUE ERR:WRONGVALUE Return when a required value is not the expected value.
EXPIRED ERR:EXPIRED Returned when a requested has been performed too late.
TOO_EARLY ERR:TOOEARLY Returned when a request has been performed too early.
TOO_MANY ERR:TOOMANY Could be return in combination with a 429 Too Many Requests Response Status.
QUOTA_REACHED ERR:QUOTAREACHED Returned when a specific quota has been reached.
NOT_FOUND ERR:NOTFOUND Returned when a resource cannot be found.
OFFLINE ERR:INTERNETDISCONNECTED Could be used to throw errors when user is offline.

Extending the `enum` in your project

For obvious reasons the default ErrorCode provided by this library might be not enough to cover all the error cases in your project.

To fill this gap, you can "extend" the ErrorCode enum by doing so:

// myproject/src/error-code.ts
import { ErrorCode as Exception } from '@alessiofrittoli/exception/code'

/** Your project custom `ErrorCode`. */
export enum MyProjectErrorCode
{
  INVALID_SIGN = 'ERR:INVALIDSIGN',
}

const ErrorCode = { Exception, MyProjectErrorCode }
type ErrorCode = MergedEnumValue<typeof ErrorCode>

export default ErrorCode

⚠️ The Type MergedEnumValue<T> is globally delcared from @alessiofrittoli/type-utils so make sure to install it if needed.


Examples

Using the default ErrorCode to throw a new Exception
import { Exception } from '@alessiofrittoli/exception'
import { ErrorCode } from '@alessiofrittoli/exception/code'

throw new Exception( 'Password is a required field to log you in.', {
  code    : ErrorCode.EMPTY_VALUE,
  status  : 422,
} )
Using custom ErrorCode to throw a new Exception
import { Exception } from '@alessiofrittoli/exception'
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`

throw new Exception( 'Invalid signature.', {
  code    : ErrorCode.MyProjectErrorCode.INVALID_SIGN,
  status  : 403,
} )
Using ErrorCode to handle errors
import { ErrorCode } from '@/error-code' // previously created in `myproject/src/error-code.ts`

try {
  ...
} catch ( error ) {
  if ( Exception.isException<string, ErrorCode>( error ) ) {
    switch ( error.code ) { // `error.code` is now type of `ErrorCode` (custom).
      case ErrorCode.MyProjectErrorCode.INVALID_SIGN:
        console.log( 'The signature is not valid.' )
        break
      case ErrorCode.Exception.UNKNOWN:
      default:
        console.log( 'Unexpected error occured.' )
    }
  }
}

Development

Install depenendencies

npm install

or using pnpm

pnpm i

Build the source code

Run the following command to test and build code for distribution.

pnpm build

ESLint

warnings / errors check.

pnpm lint

Jest

Run all the defined test suites by running the following:

# Run tests and watch file changes.
pnpm test:watch

# Run tests in a CI environment.
pnpm test:ci

Run tests with coverage.

An HTTP server is then started to serve coverage files from ./coverage folder.

⚠️ You may see a blank page the first time you run this command. Simply refresh the browser to see the updates.

test:coverage:serve

Contributing

Contributions are truly welcome!

Please refer to the Contributing Doc for more information on how to start contributing to this project.

Help keep this project up to date with GitHub Sponsor.

GitHub Sponsor


Security

If you believe you have found a security vulnerability, we encourage you to responsibly disclose this and NOT open a public issue. We will investigate all legitimate reports. Email security@alessiofrittoli.it to disclose any security vulnerabilities.

Made with ☕

avatar
Alessio Frittoli
https://alessiofrittoli.it | info@alessiofrittoli.it