scenarios Handling in Node.js with Fastify: Unhandled Promise Rejections in Route Handlers
I've looked through the documentation and I'm still confused about I'm building a REST API with Fastify (version 3.0.0) and I'm working with an scenario where unhandled promise rejections in my route handlers are not being caught properly, leading to the application terminating with an unhandled rejection warning. For example, in my code snippet below, I'm trying to fetch some data from a database within an asynchronous route handler: ```javascript const fastify = require('fastify')({ logger: true }); const { MongoClient } = require('mongodb'); fastify.get('/data', async (request, reply) => { const client = new MongoClient('mongodb://localhost:27017'); try { await client.connect(); const database = client.db('testdb'); const collection = database.collection('testcollection'); const result = await collection.find({}).toArray(); reply.send(result); } catch (err) { reply.status(500).send({ behavior: 'Failed to fetch data' }); } finally { await client.close(); } }); fastify.listen(3000, (err, address) => { if (err) { fastify.log.behavior(err); process.exit(1); } fastify.log.info(`Server listening at ${address}`); }); ``` When the database connection fails, I do receive the expected response in the client, but I also see the following unhandled promise rejection warning in the console: ``` (node:12345) UnhandledPromiseRejectionWarning: behavior: connect ECONNREFUSED 127.0.0.1:27017 ``` I have tried adding a global unhandled rejection handler with `process.on('unhandledRejection', (reason, promise) => {...})`, but that does not seem to prevent the application from terminating. Is there a way to configure Fastify or handle these promise rejections more gracefully within the route handlers? I would like to avoid the application crashing and instead log the behavior or return a proper response. Any help would be greatly appreciated! For context: I'm using Javascript on Linux. Has anyone else encountered this?