Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fernandomoraes/common-exceptions
common exceptions for nodejs
https://github.com/fernandomoraes/common-exceptions
Last synced: 13 days ago
JSON representation
common exceptions for nodejs
- Host: GitHub
- URL: https://github.com/fernandomoraes/common-exceptions
- Owner: fernandomoraes
- Created: 2016-12-17T19:55:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2016-12-17T21:06:27.000Z (almost 8 years ago)
- Last Synced: 2024-10-15T09:28:21.896Z (22 days ago)
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
common-exceptions
=========Common exceptions to help handle easily exceptions in nodejs application
## Installation
`npm install --save common-exceptions`
## Usage
```javascript
var exceptions = require('common-exceptions');
var EntityNotFoundException = exceptions.EntityNotFoundException;try {
foo();
} catch (err) {
console.log(err instanceof EntityNotFoundException); //logs true
}function foo() {
throw new EntityNotFoundException('Not found entity with id 2');
}
```## Exceptions
Currently there are the following exceptions for use:
* EntityNotFoundException
* ValidationException
* IllegalArgumentException## Examples
### Global express exception handler
```javascript
var app = require('express').app();
var ValidationException = require('common-exceptions').ValidationException;app.post('/users', (req, res) => {
let user = req.body;
if (user.age <= 18) {
throw new ValidationException('users must be over 18 years old!');
}
});app.use((err, req, res, next) => {
if (err instanceof ValidationException) {
res.status(422).send({ message: err.message });
}
});
```## TODO
* Add tests
* Allow to register new exceptions in runtime