guisehn / express-async-await-errors

Make Express.js handle async errors graciously

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Express Async Await Errors

Make Express.js handle async errors graciously.

Installation

npm install express-async-await-errors --save

Usage

'use strict'

const express = require('express')
const app = express()

const { catchAsyncErrorsOnRouter } = require('express-async-await-errors')
catchAsyncErrorsOnRouter(app) // you can also pass an express.Router() instead of the app

app.get('/', async (req, res) => {
  throw new Error("I'm an asynchronous error being caught!")
})

app.use(function(err, req, res, next) {
  res.status(500).send(`
    <h1>Server Error</h1>
    <pre>${err.stack}</pre>
  `)
})

app.listen(3000, () => console.log(`Example app listening on port 3000!`))

About

Make Express.js handle async errors graciously

License:MIT License


Languages

Language:JavaScript 100.0%