Skip to content

Instantly share code, notes, and snippets.

@QuotableWater7
Last active November 12, 2019 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuotableWater7/71f85e2ea7f82c429e947e21596abf98 to your computer and use it in GitHub Desktop.
Save QuotableWater7/71f85e2ea7f82c429e947e21596abf98 to your computer and use it in GitHub Desktop.
Examples of express behavior for TypeErrors
'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