Skip to content

Error messages can be hard to understand #1619

Open
@ericandrewlewis

Description

@ericandrewlewis

Hi there, thanks for maintaining this the postgres library, it's quite nice.

I wonder if error messages could give more context.

Let's step into the Postgres console with the provided schema

CREATE DATABASE people;
\c people
CREATE TABLE person (
   id BIGSERIAL PRIMARY KEY,
  name TEXT,
  age INTEGER
);

If I run the invalid query

INSERT INTO person(name, age) VALUES('Eric', 'thirty-one') RETURNING *;

Postgres tells me not just the error, but points to the place in my code where the error came from

ERROR:  invalid input syntax for integer: "thirty-one"
LINE 1: INSERT INTO person(name, age) VALUES('Eric', 'thirty-one') R...
                                                     ^

This is useful, because I notice I provided 'thirty-one' where an integer was expected.

I run the same INSERT INTO statement with node-postgres with this code

// pg-test.js
const { Pool, Client } = require("pg");

const client = new Client();

const main = async () => {
  await client.connect();

  const text = "INSERT INTO person(name, age) VALUES($1, $2) RETURNING *";
  const values = ["Eric", "thirty-one"];

  client.query(text, values, (err, res) => {
    console.log(err ? err.stack : res.rows[0]);
    client.end();
  });
};

main();

and get a different error stack

error: invalid input syntax for integer: "thirty-one"
    at Connection.parseE (/Users/ericlewis/Desktop/jjj/node_modules/pg/lib/connection.js:545:11)
    at Connection.parseMessage (/Users/ericlewis/Desktop/jjj/node_modules/pg/lib/connection.js:370:19)
    at Socket.<anonymous> (/Users/ericlewis/Desktop/jjj/node_modules/pg/lib/connection.js:113:22)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:269:12)
    at readableAddChunk (_stream_readable.js:256:11)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onread (net.js:581:20)

I assume it might be possible to configure node-postgres to output long stack traces as pg-promise can which might inform you what line of your code the problem started from.

To think about the problem from a higher level, I wonder what error we would want to see as developers here.

Something like this might be nice:

Error in Postgres query:
    INSERT INTO person(name, age) VALUES('Eric', 'thirty-one')
age column is of type INTEGER, TEXT provided ('thirty-one')

Invoked at pg-test.js, line 11:
  client.query(text, values, (err, res) => {

// maybe huge stack trace here

I don't have any particular recommendation here, just want to ask: do you think there are ways to improve the error messages?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions