knex.on('query-error', ...) does not trigger on some connection errors
Environment
Knex version: 0.16.3 Database + version: at least pg OS: probably all
Bug
- 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();
- Error message
Query rejects with some error about not being able to connect to DB.
- 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.
it would be nice if there were a separate event to detect connection problems. But having this check would be also great.
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 Hi, having the same issue did you add some type of retry within that event listner?
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
}