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

StackOverflow https://stackoverflow.com/questions/23510260

  •  16-07-2023
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top