node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Feature: Throw proper errors

Open shellscape opened this issue 10 months ago • 3 comments

Current modern practice for the Node ecosystem is to inherit from Error and use the .code property to distinguish between errors. node-postgres does not. Like many today, we experienced an issue with Supabase downtime. And so we've started to look at how we can make our error handling in certain scenarios more robust.

Here's an example of how we currently have to track/check for errors: https://github.com/brianc/node-postgres/blob/732580782ffa2f9299f7112ff27cfa4534cd22f3/packages/pg/test/integration/client/connection-timeout-tests.js#L82-L86

Documentation for error.code https://nodejs.org/docs/v22.14.0/api/errors.html#errorcode

The error.code property is a string label that identifies the kind of error. error.code is the most stable way to identify an error. It will only change between major versions of Node.js. In contrast, error.message strings may change between any versions of Node.js.

At the least, it would be really nice to have canonical errors that we could easily look for and react to.

shellscape avatar Feb 11 '25 20:02 shellscape

Yup I dig the idea - interested in doing a PR? Only thing to note: if the database driver recieves an error from postgres directly it will have a .code on it already - that code comes from postgres. So in that case...you can use that code. I do see how the timeout errors and other things are hard to differentiate though!

brianc avatar Feb 11 '25 22:02 brianc

Sweet. Yeah I can work this into my day job. Will try and get something in this week.

shellscape avatar Feb 11 '25 23:02 shellscape

Sweet. Yeah I can work this into my day job. Will try and get something in this week.

that would be awesome. :)

brianc avatar Feb 12 '25 02:02 brianc