Created
July 17, 2019 16:11
-
-
Save carlozamagni/577f1f0d943c4aef2d156a6e27b25fe9 to your computer and use it in GitHub Desktop.
Samples on error handling in Fastify handlers
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 fastify = require('fastify')({ | |
logger: { | |
serializers: { | |
req: function (req) { | |
return { path: req.url, headers: req.headers, payload: req.body } | |
} | |
} | |
} | |
}) | |
fastify.addHook('onError', async (request, reply, error) => { | |
console.log('ERROR HOOK FIRED') | |
console.error(error) | |
return | |
}) | |
/* | |
fastify.setErrorHandler((error, request, reply)=>{ | |
// Note that Fastify doesn't catch uncaught errors within callback | |
// based routes for you, so any uncaught errors will result in a crash. | |
// If routes are declared as async though - the error will safely | |
// be caught by the promise and routed to the default error handler | |
// of Fastify for a generic Internal Server Error response. | |
// For customizing this behavior, you should use setErrorHandler. | |
console.log('SHIT HAPPENS') | |
// report to STDOUT | |
fastify.log.error(error) | |
reply.send(error) | |
}) | |
*/ | |
const myMod = async (shouldICrash) => { | |
if (shouldICrash) throw new Error('BOOM') | |
return 0 | |
} | |
const myOthMod = (shouldICrash) => { | |
return new Promise((resolve, reject) => { | |
if (shouldICrash) return reject(new Error('BOOM')) | |
resolve(0) | |
}) | |
} | |
// HAPPY CASE PROMISE/CALLBACK | |
fastify.get('/', function (request, reply) { | |
reply.send({ hello: 'world' }) | |
}) | |
// HAPPY CASE ASYNC | |
fastify.get('/asyncok', async (request, reply) => { | |
const res = await myMod(false) | |
reply.send(res) | |
}) | |
// ERROR CASES | |
// ERROR CASES | |
// ERROR CASES | |
// OK | |
fastify.get('/asyncerr', async (request, reply) => { | |
const res = await myMod(true) | |
reply.send(res) | |
}) | |
// OK (but results in a process crash) | |
fastify.get('/err', (request, reply) => { | |
// uncaught error | |
throw new Error('boom') | |
reply.send(res) | |
}) | |
// OK | |
fastify.get('/caughterr', (request, reply) => { | |
// result is 500 | |
reply.send(Error('BOOM')) | |
}) | |
// NO (results in a leaked descriptor) | |
fastify.get('/promiseerr', (request, reply) => { | |
myOthMod(true).then(res => { | |
reply.send(res) | |
}) // uncaught promise on sync handler | |
}) | |
// YES | |
fastify.get('/asyncpromiseerr', async (request, reply) => { | |
const res = await myOthMod(true) // uncaught promise on sync handler | |
reply.send(res) | |
}) | |
fastify.listen(3000, function (err, address) { | |
if (err) { | |
fastify.log.error(err) | |
process.exit(1) | |
} | |
fastify.log.info(`server listening on ${address}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment