knex icon indicating copy to clipboard operation
knex copied to clipboard

knex.on('query-error', ...) does not trigger on some connection errors

Open elhigu opened this issue 6 years ago • 4 comments

Environment

Knex version: 0.16.3 Database + version: at least pg OS: probably all

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do
knex = require('knex')({ client : 'pg', connection: 'postgres:///invaliddb' });

// event is not triggered for connection error
knex.on('query-error', e => console.dir(e));

knex.raw('select 1').then();

  1. Error message

Query rejects with some error about not being able to connect to DB.

  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to mysql or postgresql, then single file example which initializes needed data and demonstrates the problem.

Code is above.

elhigu avatar Mar 20 '19 09:03 elhigu

it would be nice if there were a separate event to detect connection problems. But having this check would be also great.

lfernando-silva avatar Apr 24 '19 14:04 lfernando-silva

whoa, im not alone. tried to make auto-retry functionality for this....

If this not works, im going to create common retry function and scope entire query process to be retried .

bordeux avatar Mar 07 '23 11:03 bordeux

@bordeux Hi, having the same issue did you add some type of retry within that event listner?

andrewkaldani30 avatar Apr 23 '24 16:04 andrewkaldani30

i crafted outside knex library. I wrapped all queries executions with retry function like

  const result = await queryRetry(knex =>  knex.raw(
      `select * from yourTable`
    );
  );
  

queryRetry function automatically catching errors, and if there is connection isssue, it repeats the query 3 times.

What exceptions im catching:

        if (
        error?.code === 'ETIMEDOUT' ||
        error?.code === 'ECONNREFUSED' ||
        error?.code === 'ER_NOT_SUPPORTED_AUTH_MODE' ||
        error?.message === 'timeout expired' ||
        error?.syscall === 'connect' ||
        error?.message?.includes('ECONNREFUSED')
      ) {
        //retry
      }


bordeux avatar Apr 23 '24 18:04 bordeux