Pg-promise: Get query that caused error?

Created on 20 May 2016  路  10Comments  路  Source: vitaly-t/pg-promise

I'm trying to keep a log of errors for a program that I'll be running with a cron job, but I'm not sure how to get the query that triggered the error into the log in a good way.

Would it be possible to add the query that caused the error to the error object?

For example this is my current error:

{
  error: permission denied for relation log
    at Connection.parseE (/dir/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/dir/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/dir/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:172:18)
    at Socket.Readable.push (_stream_readable.js:130:10)
    at TCP.onread (net.js:542:20)
  name: 'error',
  length: 133,
  severity: 'ERROR',
  code: '42501',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: '/home/awsrsqa/padb/src/pg/src/backend/catalog/aclchk.c',
  line: '1484',
  routine: 'aclcheck_error'
}

it'd be nice to add a field like

{
  // ...
  queryThatCausedIt: 'SELECT * FROM log'
 // ...
}
question

Most helpful comment

To make a better output string inside error(err, e):

var EOL = require('os').EOL;

var errMsg = (err.message || err) + EOL + JSON.stringify(e);

But if you want only error + the query, then:

var errMsg = (err.message || err) + EOL + e.query;

All 10 comments

  • You have all the details reported within the error event, is where you should be logging it.
  • Standard rejects never modify the original reject data.

@vitaly-t ah alright. So is it different from the error object I posted here, or is there specific reason why I'm not getting the info here?

This is what caused it:

return db.tx(function(t) {
  return t.batch([
    t.none('CREATE TABLE IF NOT EXISTS $1:value($2:value)', [name, types.join(',\n')]),
    t.none('INSERT INTO $2:value($3:value) VALUES $1', [data, name, cols.join(',')]),
    t.none('GRANT SELECT ON $1:value to readonly', name)
  ]);
});

There is no other info there. Only the event error has all the context in it.

And is it only available inside of the pg initialization or is there somewhere else I can access it?

I'm not even sure what you mean by that. That's the global error handler for the entire library, that's all.

Everything that goes wrong is reported there - is the perfect place to log your errors.

@vitaly-t oh nevermind 馃槄 thank you!

Apologies for hijacking but is there a way to aggregate the error info available in the error event, package it up in a custom my_error object that can be sent to client?

This is what mine ended up looking like:

// Configure the database connection
export const pgp = postgres({
  promiseLib: Promise,
  error(err, e) {
    if (process.env.NODE_ENV === 'production') {
      reportError(`${err}\n${JSON.stringify(e, null, 2)}`);
    } else {
      console.log(err, e);
    }
  }
});

To make a better output string inside error(err, e):

var EOL = require('os').EOL;

var errMsg = (err.message || err) + EOL + JSON.stringify(e);

But if you want only error + the query, then:

var errMsg = (err.message || err) + EOL + e.query;

Very good. Thanks guys!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanh1414 picture seanh1414  路  4Comments

illarionvk picture illarionvk  路  3Comments

Fte-github picture Fte-github  路  4Comments

jabooth picture jabooth  路  4Comments

dzaman picture dzaman  路  3Comments