Last active
November 12, 2019 16:58
-
-
Save QuotableWater7/71f85e2ea7f82c429e947e21596abf98 to your computer and use it in GitHub Desktop.
Examples of express behavior for TypeErrors
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
'use strict' | |
const express = require('express') | |
const request = require('request') | |
const configureRoute = handler => async (req, res, next) => { | |
try { | |
handler(req, res, next) | |
} catch (error) { | |
// we successfully caught an error | |
res.send('Successfully caught the error') | |
} | |
} | |
function handlerWithoutCallstack(data, next) { | |
callbackStyleFunction({}, next) | |
} | |
function handlerWithCallstack(data, next) { | |
request('http://google.com', () => { | |
callbackStyleFunction({}, next) | |
}) | |
} | |
function callbackStyleFunction(data, next) { | |
// TypeError because "missingProperty" is undefined" | |
next(null, data.missingProperty.boom) | |
} | |
const app = express() | |
// this error is successfully caught because it is | |
// in same callstack as the route handler | |
app.use('/no-callstack', configureRoute(handlerWithoutCallstack)) | |
// this error is not successfully caught because it is | |
// in a different callstack than the route handler | |
app.use('/callstack', configureRoute(handlerWithCallstack)) | |
app.listen(3333, () => { | |
console.log('server up...') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment