Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nascentdigital/errors
A reusable set of Javascript / Typescript errors for browser and node applications.
https://github.com/nascentdigital/errors
Last synced: about 2 months ago
JSON representation
A reusable set of Javascript / Typescript errors for browser and node applications.
- Host: GitHub
- URL: https://github.com/nascentdigital/errors
- Owner: nascentdigital
- License: mit
- Created: 2020-05-23T01:46:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-22T15:15:07.000Z (8 months ago)
- Last Synced: 2024-09-24T13:36:22.059Z (3 months ago)
- Language: TypeScript
- Size: 622 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Errors
> A set of Typescript and Javascript `Error` subclasses to simplify strongly typed (or prototyped)
application error handling.[![NPM](https://img.shields.io/npm/v/@nascentdigital/errors.svg)](https://www.npmjs.com/package/@nascentdigital/errors)
## Features
- Provides a reusable set of common `Error` subclasses that can be leveraged for convenience.
- Simple base class that you can just extend for your own custom errros.
- Maintains the proper prototype chain so that your custom errors show up correctly in a stacktrace.
- Javascript support in Node.js and browsers.
- Typescript 3 support.## Installation
```sh
$ npm i -s @nascentdigital/errors
```## Usage
### Leveraging existing errors
Just import the errors that you need and start throwing!
```javascript
import {ArgumentError} from "@nascentdigtal/errors";function failure(expected) {
// validate arguments
if (!expected) {
throw new ArgumentError("expected");
}// ...
}
```### Creating custom errors
Creating your own custom errors is just as easy.
Simply extend the base `RuntimeError`, ensuring you call the base method.```javascript
import {RuntimeError} from "@nascentdigtal/errors";// define simple error (hands free)
export class SimpleError extends RuntimeError {}// define complex error
export class ComplexError extends RuntimeError {constructor(message, value) {
// call base constructor (does the heavy lifting)
super(message);// do your stuff
this.value = value;
}
}function deathwish() {
throw new ComplexError("This isn't smart", -1);
}
```