Skip to content

How to handle errors properly? #2492

Open
@DJDaveMark

Description

@DJDaveMark

Is this still the correct way to handle errors?

We wrote this code back in January 2017 when we were using ^4.4.3 using pg.connect before refactoring it to use pool.connect when we upgraded to use ^7.11.0 back in June 2019

pool.connect(function (err, client, done) {
    if (err) {
        done(err); // done may be called multiple times with no side-effects
        console.error('could not connect to postgres', err);
        callback(err);
        return;
    }

    // do something
});

today we started getting the following error:

TypeError: done is not a function

pointing to the 3rd line of the code above.

I see from the current documentation that done is not called in case of an error:

pool.connect((err, client, done) => {
  if (err) throw err
  client.query('SELECT * FROM users WHERE id = $1', [1], (err, res) => {
    done()
    if (err) {
      console.log(err.stack)
    } else {
      console.log(res.rows[0])
    }
  })
})

But I can't find any guidelines in the current documentation on how are you supposed to handle errors (in ^7.11.0 at least).

Can anyone provide an example?

Note: we are using old-style callbacks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions