Created
January 7, 2022 09:16
-
-
Save Avi-E-Koenig/d40a0e6e0f04be6e452cfcf38d5b6c9a to your computer and use it in GitHub Desktop.
basic all purpose error handling wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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