try { if (!conn) { console.log("Attempting to Connect to Atlas"); conn = await MongoClient.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true, }); console.log("Successfully connected to Atlas"); } } catch (err) { console.log("Failed to connect to Atlas: ", err); return { statusCode: 500, body: JSON.stringify(err), };}
The above is my code to connect to Atlas via Node. The Atlas database has not enabled the IP address of the above server to connect. Hence it is supposed to fail. But when it does failed, the code in the catch block is not executed, here are the logs:
2022-11-28T14:10:35.966-06:00 2022-11-28T20:10:35.966Z 7e455355-8126-4f71-afb3-2185683f9f8a INFO Attempting to Connect to Atlas2022-11-28T14:10:38.725-06:00 2022-11-28T20:10:38.725Z 7e455355-8126-4f71-afb3-2185683f9f8a Task timed out after 3.01 seconds2022-11-28T14:10:38.725-06:00 END RequestId: 7e455355-8126-4f71-afb3-2185683f9f8a2022-11-28T14:10:38.725-06:00 REPORT RequestId: 7e455355-8126-4f71-afb3-2185683f9f8a Duration: 3008.53 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 49 MB2022-11-28T14:11:42.725-06:00 START RequestId: 7e455355-8126-4f71-afb3-2185683f9f8a Version: $LATEST
The question is, why doesn't it throw an error? Why doesn't it go into the catch block?
This is a lambda function on AWS attempting to connect to atlas.