For all cases, you will want every route to handle any errors that can occur so that we can notify the caller and potentially do some logging.
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
| app.get('/', (req, res) => { | |
| try { | |
| throw new Error('OH DEAR!') | |
| } catch (err) { | |
| res | |
| .status(500) | |
| .send('Something terrible has happened') | |
| } | |
| }) |
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
| app.get('/', (req, res, next) => { | |
| try { | |
| throw new Error('OH DEAR!') | |
| } catch (err) { | |
| next(err) | |
| } | |
| }) |
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
| app.use((err, req, res, done) => { | |
| // Here log any information you want to | |
| console.error(err.toString()) | |
| if (res.headersSent) { | |
| return next(err) | |
| } | |
| res | |
| .status(500) | |
| .send('An Error Occured') | |
| }) |
One really important thing to remember is to put the function after you define your route handlers! Or it won't catch the errors. (at the bottom of the file)
There are a few different types of things you might be doing in the routes so here are a few examples of how to pass the errors.
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
| app.get('/', (req, res, next) => { | |
| try { | |
| throw 'try-catch-error' | |
| } catch (err) { | |
| next(err) | |
| } | |
| }) |
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
| app.get('/callback', (req, res, next) => { | |
| fs.writeFile('blah', 'blah', err => { | |
| if (err) { | |
| return next(err) | |
| } | |
| // Happy path code | |
| }) | |
| }) |
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
| app.get('/promise', (req, res, next) => { | |
| Promise.reject('promise-error') | |
| .then(_ => { | |
| // Normal response code | |
| }) | |
| .catch(next) | |
| }) |
More on error handling strategies to come!
No comments:
Post a Comment