MikeyB
MikeyB

Reputation: 490

Is there a way for KNEX ERRORS to also log WHERE in the code they take place?

Some Knex errors log the file and line in which they occur, but many DO NOT. This makes debugging unnecessarily tedious. Is .catch((err)=>{console.log(err)}) supposed to take care of this? The fact that code tries to repeat around 4 times (I want it to try once and stop, absolutely no need for more attempts, ever - it only messes things up when further entries are made to the database)?

Upvotes: 2

Views: 2707

Answers (1)

Sinux
Sinux

Reputation: 1808

Some Knex errors log the file and line in which they occur, but many DO NOT

Can you give us some of your query examples which silent the error?

I'm heavy Knex user, during my development, almost all errors show which file and line they occurred unless two kind of situations:

  1. query in transaction which may complete early.

In this situation, we have to customize knex inner catch logic and do some knex injection such as Runner.prototype.query, identify the transactionEarlyCompletedError, and log more info: sql or bindings on catch clause.

  1. pool connection error

such as mysql error: Knex:Error Pool2 - Error: Pool.release(): Resource not member of pool

this is another question which depends on your database env and connection package.

The fact that code tries to repeat around 4 times

  1. if your repeat code written in Promise chain,I don't think it will throw 4 times, it should blows up at the first throw.
query1
.then(query2)
.then(query3)
.then(query4)
.catch(err => {})
  1. concurrently executed queries

If any promise in the array is rejected, or any promise returned by the mapper function is rejected, the returned promise is rejected as well.

Promise.map(queries, (query) => {
  return query.execute()
  .then()
  .catch((err) => {
    return err;
  })
}, { concurrency: 4})
.catch((err) => {
  // handle error here
})
  1. if you use try catch and async await

still it would not repeat 4 times, if you already know the error type, meanwhile, if you don't know what error will throw, why don't you execute it only once to find out the error?

async function repeatInsert(retryTimes = 0) {  
  try {
    await knex.insert().into();
  } catch(err) {
    // handle known error
    if (err.isKnown) {
      throw err;
    }
    // and retry
    if (retryTimes < 4) {
      return await repeatInsert(retryTimes + 1);
    }
  }
}

Upvotes: 2

Related Questions