brianc/node-postgres

Error object differs between pg and pg-native

deyhle opened this issue · 3 comments

node-postgres abstracts over the pg-native module to provide exactly the same interface as the pure JavaScript version. No other code changes are required. If you find yourself having to change code other than the require statement when switching from require('pg') to require('pg').native please report an issue.

The fix for #743 made me test a switch to pg-native again. However, I still found an API difference: the Error object. While the default pg library returns an error with properties code and detail, the parameters are named differently when using pg-native.

require('pg'):

{
  "name": "error",
  "length": 199,
  "severity": "ERROR",
  "code": "23505",
  "detail": "Key (c1)=(one) already exists.",
  "schema": "public",
  "table": "dbhandlertest",
  "constraint": "dbhandlertest_pkey",
  "file": "nbtinsert.c",
  "line": "402",
  "routine": "_bt_check_unique"
}

require('pg').native:

{
  "severity": "ERROR",
  "sqlState": "23505",
  "messagePrimary": "duplicate key value violates unique constraint \"dbhandlertest_pkey\"",
  "messageDetail": "Key (c1)=(one) already exists.",
  "sourceFile": "nbtinsert.c",
  "sourceLine": "402",
  "sourceFunction": "_bt_check_unique"
}

This makes it hard to do proper error handling. Especially the code property is probably used a lot for that.

Adding to the pg@7.0 release milestone to normalize these fields across the native bindings & the pure javascript library.

This makes it hard to do proper error handling. Especially the code property is probably used a lot for that.

In the meantime, the generic error handler can simply accept the native flag and parse errors differently.

I have this fixed on the 7.0 branch.