Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fatmatto/throwable-http-errors
Throwable HTTP errors for node apis
https://github.com/fatmatto/throwable-http-errors
errors es6 http nodejs
Last synced: 10 days ago
JSON representation
Throwable HTTP errors for node apis
- Host: GitHub
- URL: https://github.com/fatmatto/throwable-http-errors
- Owner: fatmatto
- License: mit
- Created: 2018-03-01T08:12:07.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T07:10:27.000Z (over 1 year ago)
- Last Synced: 2024-04-08T16:45:43.684Z (10 months ago)
- Topics: errors, es6, http, nodejs
- Language: JavaScript
- Size: 572 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/fatmatto/throwable-http-errors.svg?branch=master)](https://travis-ci.org/fatmatto/throwable-http-errors)
# Throwable HTTP errors
Throwable HTTP errors for node APIs.
## Installation
```bash
npm i throwable-http-errors
```## Usage
```javascript
const Errors = require('throwable-http-errors')// for example, using express.js
router.post('/', wrap(async (req, res, next) => {
try {
if (!validatePet(req.body)) {
throw new Errors.BadRequest()
}const pet = await PetsController.create(req.body)
res.send({ status: true, data: pet })
} catch (e) {
return next(e)
}
}))
```## Reason
Before async/await, I was used to send errors to the main error catching middleware just by running
```javascript
return next(new Errors.BadRequest())
```Now, with async/await we can leverage try/catch blocks to handle errors. While I'm not quite happy with having try/catch blocks in every route, having **await** for promises improves code readablity (and quality in my opinion) a lot.