Node-postgres: Error line number inaccurate

Created on 19 Mar 2016  路  4Comments  路  Source: brianc/node-postgres

env

  • postgres - 9.4 on Ubuntu Precise 64
  • node-postgres - 4.5.1 on Mac OSX
  • node.js - 4.3.2

    problem

"inaccurate" might be an understatement. My query is 9 lines long. When I remove a comma from the select clause in order to generate an error, much of the error content comes back correct, even position which appears to be relatively accurate. On the other hand, I get 1052 as the lune number for the error, which is clearly off. I'm assuming there's an issue in the parseE error parser, but haven't investigated much further yet.

Additional note: this error occurs identically if the query is parameterized as well

Most helpful comment

So good news, the position is calculated _prior_ to interpolating the parameterized query. This means you can essentially calculate the line number like this (I'll leave it to others to optimize):

// assuming `query` is the original query passed to pg, parameterized or otherwise
client.query(query, function(err) {
  if (err) {
    console.error('line : %s', ((query.substring(0, err.position).match(/\n/g) || []).length + 1));
  }
});

I'll close this and leave these comments as a pointer to anyone else wanting to get the query's line number for a given error.

All 4 comments

upon further inspection, this line number appears to be pointing back to the postgres source code, not the query. It's pointing to a section in the lexer that is throwing the syntax error I'm generating. So I guess I have 2 questions:

  1. Is this expected behavior?
  2. If it is, how do I get the line number of a postgres error using node-postgres?

It's always been like this, PostgreSQL only reports code lines inside its C++ parser, and it never reports line numbers for the SQL query, it only provides a description.

Most likely you can't get any more details than that.

You're right, it's more clear in the postgres docs.

Given the position value, though, one should be able to determine it, _if_ the "original query string" it refers to in the docs for position is the same whether a query is parameterized or not. In other words, does the parameterization occur before or after the position is determined, and if it's after, then I don't think I could readily determine the line at that point reliably.

Anyone know off hand if the position is determined before or after parameterization? If not, I'll test later and report back.

So good news, the position is calculated _prior_ to interpolating the parameterized query. This means you can essentially calculate the line number like this (I'll leave it to others to optimize):

// assuming `query` is the original query passed to pg, parameterized or otherwise
client.query(query, function(err) {
  if (err) {
    console.error('line : %s', ((query.substring(0, err.position).match(/\n/g) || []).length + 1));
  }
});

I'll close this and leave these comments as a pointer to anyone else wanting to get the query's line number for a given error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wrod7 picture wrod7  路  4Comments

gpanainte picture gpanainte  路  3Comments

joaquimknox picture joaquimknox  路  3Comments

Cosrnos picture Cosrnos  路  3Comments

chrisjensen picture chrisjensen  路  4Comments