typesafe-exception
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

Type-safe Exception

Because Error class in JavaScript could have been designed better.

Simple serialization-friendly Exception class, with opt-in type-safe details, numeric status code and custom fields.

Examples

import { Exception } from 'typesafe-exception';

// Sometimes it's handy to put all app errors in `errors.ts`, for better centralization and visibility

// Error `name` will be inferred from class name
export class ValidationError extends Exception {
    // Optionally, numeric `status` can be assigned
    status = 400;
}

// Optionally, `details` field can have types

export class AccessDeniedError extends Exception<{ reason: string }> {
    status = 403;
}

throw new AccessDeniedError('You are not allowed here', { reason: 'too boring' });

Package Sidebar

Install

npm i typesafe-exception

Weekly Downloads

0

Version

2.0.4

License

ISC

Unpacked Size

2.99 kB

Total Files

6

Last publish

Collaborators

  • inca