Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Created January 7, 2022 09:16
Show Gist options
  • Save Avi-E-Koenig/d40a0e6e0f04be6e452cfcf38d5b6c9a to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/d40a0e6e0f04be6e452cfcf38d5b6c9a to your computer and use it in GitHub Desktop.
basic all purpose error handling wrapper
const express = require('express')
const app = express()
const port = 3000
const catchErrors = (requestHandler) => {
return async (req, res, next) => {
try {
throw 'oops'
return await requestHandler(req, res, next);
} catch (error) {
next(error);
}
};
};
const helloController = (req, res) => {
res.send('Hello World!')
}
app.get('/', catchErrors(helloController))
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment