Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active July 21, 2019 00:45
Show Gist options
  • Save chrisdel101/d558631083cecac60d5b7cac9dcf037a to your computer and use it in GitHub Desktop.
Save chrisdel101/d558631083cecac60d5b7cac9dcf037a to your computer and use it in GitHub Desktop.
error handling issue
// call to api
httpCall: async url => {
return new Promise((resolve, reject) => {
http.get(url, res => {
res.setEncoding('utf8')
res.on('data', d => {
resolve(d)
})
})
})
}
// call http method above
fetchData: async url => {
const call = module.exports.httpCall(url)
let remoteJson = await call
return remoteJson
}
// ==== controller render
render: async (ctx, next) => {
const data = JSON.parse(await utils.fetchData('http://example.com'))
// NOTE: To test I remove this so it returns junk, and all I get is a silent fail and a 404.
await ctx.render('index', {
data: data
})
}
// ==== routes index
const { catchErrors } = require('../errorHandlers')
router.get('/', catchErrors(indexController.render))
// === errorHandler - version based on your code
exports.catchErrors = fn => {
return function(ctx, next) {
return fn(ctx.request, ctx.response, next).catch(next)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment