Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samislam/catch-async-wrapper-express
middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers
https://github.com/samislam/catch-async-wrapper-express
Last synced: 1 day ago
JSON representation
middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers
- Host: GitHub
- URL: https://github.com/samislam/catch-async-wrapper-express
- Owner: samislam
- Created: 2022-12-25T13:52:59.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-12T16:03:52.000Z (11 months ago)
- Last Synced: 2024-09-18T20:24:28.941Z (2 months ago)
- Language: TypeScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# catch-async-wrapper-express
a simple middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers.
# Installation
```
npm i catch-async-wrapper-express
``````js
const catchAsync = require('catch-async-wrapper-express')
```# Requirements:
- [expressjs](https://www.npmjs.com/package/express).
- [Nodejs](https://nodejs.org/).# Examples:
catch any async or sync error and handle it to the `next()` function.
having the second argument as `undefined`, catchAsync will default to the express `next` function.```js
// if failed, next will be called with the error
app.get(
'/users',
catchAsync(async (req, res, next) => {
const data = await UserModel.find({})
res.status(200).json({
status: 'success',
data,
})
})
)
```You can also have a custom error handler as well by passing it as the second parameter:
```js
// if failed, localErrorHandler will be called with the errorconst localErrorHandler = (error, req, res, next) => {
console.log(error)
res.status(500).json({
status: 'fail',
message: 'Unable to create user, please try again',
})
}app.post(
'/users',
catchAsync(async (req, res, next) => {
const data = await UserModel.create(req.body) // ex: using mongoose
res.status(200).json({
status: 'success',
data,
})
}, localErrorHandler)
)
```a **local error handler** can also be an asyncronous function as well, and calling next inside of the custom error handler will move the error to [your express global error handler](https://expressjs.com/en/guide/error-handling.html) middleware to process.
```js
// if failed, localErrorHandler will be called with the errorconst localErrorHandler = async (error, req, res, next) => {
console.log('returning the money back...')
const invoice = await Invoices.findById(req.invoiceId)
await transferMoney(invoice.to, invoice.from)
res.status(500).json({
status: 'fail',
message:
"You can't buy the item because it doesn't exist in the merchant stock, please try again",
})
}app.post(
'/purchase-item',
// verify & deduct/feed balances
catchAsync(async (req, res, next) => {
const from = '[email protected]'
const to = '[email protected]'
const invoiceId = await transferMoney(from, to)
req.invoiceId = invoiceId
next()
}),
// verify & remove from stock
catchAsync(async (req, res, next) => {
await Stock.pick(req.body.itemToPurchase)
res.status(200).json({
status: 'success',
data,
})
}, localErrorHandler)
)
```If a local error handler throw an error, the error will be handled inside [your express global error handler](https://expressjs.com/en/guide/error-handling.html) middleware to process it.
```js
const localErrorHandler = (error, req, res, next) => {
throw error
}app.post(
'/users',
catchAsync(async (req, res, next) => {
const data = await UserModel.create(req.body)
res.status(200).json({
status: 'success',
data,
})
}, localErrorHandler)
)
```calling next with no arguemnts inside the local error handler will get the request back on the track of the middleware stack again. This means that you handled your error, and the request is ready to continue its journy:
```js
const localErrorHandler = async (error, req, res, next) => {
console.log('handling the error...')
await ErrorModel.create(error) // save the error to database
next()
}app.post(
'/users',
catchAsync(async (req, res, next) => {
throw new Error('Hello from the error!') // an error
}, localErrorHandler),
(req, res, next) => {
res.status(200).json({
status: 'success',
message: 'the operation was completed successfuly with (1) errors',
})
}
)
```