npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alessiofrittoli/exception

v1.1.0

Published

Handle errors with ease

Downloads

204

Readme

Exception 🚦

Version 1.1.0

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	: 'ERRNOTFOUND',
		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.

Example
try {
	throw new Exception( 'Something went wrong', { code: 'ERRUNKNOWN' } )
} catch ( error ) {
	if ( Exception.isException( error ) ) {
		// we can safely access `Exception` properties
		console.error( `Error [${ error.code }]: ${ error.message }` )
	} else {
		console.error( error )
	}
}

Usage Scenarios

Custom Error Handling

The Exception class is ideal for creating domain-specific errors with additional metadata, such as error codes and HTTP statuses.

Example
enum ErrorCode
{
	EMPTY_VALUE	= 'ERREMPTYVALUE',
	WRONG_FORMAT= 'ERRWRONGFORMAT',
	EXPIRED		= 'ERREXPIRED',
	TOO_EARLY	= 'ERRTOOEARLY',
	NOT_ALLOWED	= 'ERRNOTALLOWED',
}

try {
	await fetch( ... )
} catch ( error ) {
	// error thrown by the server: Exception( 'Wrong value.', { code: ErrorCode.WRONG_FORMAT, status: 422 } )
	if ( Exception.isException<string, ErrorCode>( error ) ) {
		console.log( error.code ) // `error.code` is type of `ErrorCode`.
	}
}

Development

Install depenendencies

npm install

or using pnpm

pnpm i

Build your source code

Run the following command to 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

# Run tests and watch file changes with jest-environment-jsdom.
pnpm test:jsdom

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

# Run tests in a CI environment with jest-environment-jsdom.
pnpm test:ci:jsdom

You can eventually run specific suits like so:

pnpm test:jest
pnpm test:jest:jsdom

Contributing

Contributions are truly welcome!
Please refer to the Contributing Doc for more information on how to start contributing to this project.


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 [email protected] to disclose any security vulnerabilities.

Made with ☕