https://github.com/hectotruj/thrower
Templated Errors
https://github.com/hectotruj/thrower
error error-handling error-messages errors node node-js node-module nodejs npm npm-module npm-package throw thrower
Last synced: about 1 month ago
JSON representation
Templated Errors
- Host: GitHub
- URL: https://github.com/hectotruj/thrower
- Owner: hectotruj
- License: mit
- Created: 2018-02-12T23:27:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-19T00:50:06.000Z (about 7 years ago)
- Last Synced: 2025-09-30T11:55:39.421Z (8 months ago)
- Topics: error, error-handling, error-messages, errors, node, node-js, node-module, nodejs, npm, npm-module, npm-package, throw, thrower
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Thrower
[](https://travis-ci.org/hectotruj/thrower)
[](https://codecov.io/gh/hectotruj/thrower)
Simplify throwing errors, write cleaner code, make testing a breeze. Plus, its fun to use: `thrower.r()`... get it? Throw Err. That's right. It's useful and it fun.
## Introduction
We feel errors have turned into more of a pain than a tool, and we want to fix that. Normally we write custom errors and then forget about them, or we wait for an indecipherable stack trace. The idea is: Find the error that best fits the condition you'd like to prevent and throw it when you meet the condition. Use and reuse errors.
## Installation
```
npm install thrower --save
```
## Usage
[Error Definitions](https://hectotruj.gitbooks.io/thrower/content/error-codes.html)
Pick the error code that best matches the error you would like to throw. For example:
### thrower.r(error : string, message? : string)
```js
const thrower = require('thrower');
insertName(name) {
if(name.length > 5) thrower.r('FIELD_TOO_LONG')
...
}
insertName('Star Lord')
/**
* throws =>
* {
* error : 'FIELD_TOO_LONG,
* description : 'Field max character limit exceeded.',
* details : 'at insertName(/home/example/index.js:3:10)'
* }
* /
```
or
```js
const thrower = require('thrower');
const MAX_FIELD_LENGTH = 5
insertName(name) {
if(name.length > MAX_FIELD_LENGTH)
thrower.r('FIELD_TOO_LONG', `Name must be less than ${MAX_FILED_LENGTH} chars.`)
...
}
insertName('Star Lord')
/**
* throws =>
* {
* error : 'FIELD_TOO_LONG,
* description' : 'Field max character limit exceeded.',
* details : 'at insertName(/home/example/index.js:5:10)',
* message : 'Name must be less than 5 chars.'
* }
* /
```
### thrower.get(error : string)
You don't have to throw our errors, you can use our description and throw your own. For example:
```js
const thrower = require('thrower');
let description = thrower.get('FIELD_MISSING')
// description => 'A required field is missing.'
```
## Constributions
Tell us how we can do better!
We love our community. We are not perfect and we don't aim to be, but we do aim to provide a tool our community loves. Feel free to open an issue to suggest new codes and definitions or methods.