Skip to content
Advertisement

Unable to catch exception inside a Promise

I am trying to write a retry logic to connect to AWS Aurora db for mysql. Aurora db can sleep if it is left idle for a specified amount of time and performs a cold restart upon receiving a request. The restart can take 30-50sec and to establish a connection, I am doing three reattempts in the code below.

To test the wait times on my local system, I am trying to simulate the sleep scenario by setting wrong port number when connection pool is being created. And the code is throwing an error as expected.

Error: connect ECONNREFUSED 127.0.0.1:3305 (3306 is the correct one.)

JavaScript

I invoke initConnection() to create the pool and then use it to query the db. The code should throw the exception but it should throw it after 3 reattempts. But this is not happening. The code is throwing error when the code written for testing the connection is invoked. (‘SHOW DATABASES;’). The code is not trying to reattempt the connection.

Can someone help point out the issue in this code and how it can be corrected?

JavaScript

Advertisement

Answer

Not able to correctly test but something like this:

JavaScript

All you had to do:

  1. Promisify the .query callback
  2. (simplify/clear/fix code a bit)
  3. Add a try-catch to your reattempt method call due to in your case if connection is established and nothing is thrown – you will still get reject('Unable to Create Connection Pool.');
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement