👋
Welcome to @joellesenne/express-validation-error
Simple Middleware to manage exceptions within express error in asynchronous
🏠 Homepage
Table of Contents
Prerequisites
This project requires node and npm. Go check them out if you don't have them locally installed.
Install
Use simple Middleware to manage exceptions for Express @joellesenne/express-async-handler
pnpm install --D @joellesenne/express-async-handler @joellesenne/express-validation-error
# OR
npm install --save-dev @joellesenne/express-async-handler @joellesenne/express-validation-error
# OR
yarn add -D @joellesenne/express-async-handler @joellesenne/express-validation-error
Usage
This code is an Express.js HTTP GET route which is using an asyncHandler. It attempts to find 'foo' using the findAll() function, and if it doesn't exist, it throws a ValidationError which can be handled by the next function. If it successfully finds 'foo', the response will send the data (bar) back to the client.
const asyncHandler = require('@joellesenne/express-async-handler')
const ValidationError = require('@joellesenne/express-validation-error')
express.get('/', asyncHandler(async (req, res, next) => {
try {
const bar = await foo.findAll();
if (!bar) {
return next(new ValidationError("No bar valid", 404));
}
res.send(bar)
} catch (error) {
return next(error);
}
}))
Run tests
mocha test/index.test.js
Author
- Website: https://joellesenne.dev
- Twitter: @joellesenne
- Github: @joellesenne
- LinkedIn: @joellesenne
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Show your support
Give a
📝 License
Copyright © 2021 Joël Lesenne.
This project is MIT licensed.