Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the SQL statement together with the error returned by node-postgres?

I get errors that look like this from node-postgres (when calling its client.query method):

{ [Error: syntax error at or near "as"]
 severity: 'ERROR',
 code: '42601',
 position: '60',
 file: 'scan.l',
 line: '1044',
 routine: 'scanner_yyerror' } }

If I could see the offending SQL alongside the error message, it would make debugging a lot easier. Is it possible to get it somehow?

I realise that I can implement this manually (wrap client.query etc.), but what I'm interested in is whether node-postgres itself can be coerced into providing the SQL.

like image 591
Alex Korban Avatar asked Oct 24 '25 07:10

Alex Korban


1 Answers

Looking at the source code (https://github.com/brianc/node-postgres/blob/master/lib/client.js#L331), the query method returns a Query object. So you should be able to do something like this:

var query = client.query('SELECT $1::int AS number', ['1'], function(err, result) {
  if (err) {
    var sql = query.text;
  }
}

(assuming your callback function is in the same scope as query).

Disclaimer: I've never used node-postgres.

like image 190
Ivan Vergiliev Avatar answered Oct 25 '25 21:10

Ivan Vergiliev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!