Repository: brianc/node-postgres (language: javascript)

Issue:
Export DatabaseError in pg package
I wanted to catch errors I got from pg in order to improve my project's reporting, but found out that I needed to import pg-protocol package to get DatabaseError type.

This seems a little bit illogical: I think that if I interact with a package, it should export not only the type definitions, but also the errors it's going to throw up to me.

Another reason to do this is that now I have both node-postgres and pg-protocol in my package.json, and, for example, if I update node-postgres to a new version and forget to update pg-protocol, the actual runtime type of the DatabaseError I catch may change under me, and my code that's depending on the DatabaseError type imported from outdated pg-protocol package would silently cease to work.

I also wanted to point out that documentation seems to be a bit lacking on that regard, and doesn't mention any errors that I should be catching as a user of pg package.

If you approve such changes in principle, I would be happy to contribute PR implementing them, both re-export and docs (will create a separate issue to track docs progress).


Discussion / hints:
so you want the main `pg` package to export `DatabaseError`?  that sounds reasonable
I am trying to read the `code` property of the error object returned when something fails, how can I do that in typescript using `pg`? Is this what this issue is referring to? Is there a workaround in the mean time?

Fix patch:
diff --git a/packages/pg/lib/index.js b/packages/pg/lib/index.js
index 47eca1fd0..7f02abab5 100644
--- a/packages/pg/lib/index.js
+++ b/packages/pg/lib/index.js
@@ -4,6 +4,7 @@ var Client = require('./client')
 var defaults = require('./defaults')
 var Connection = require('./connection')
 var Pool = require('pg-pool')
+const { DatabaseError } = require('pg-protocol')
 
 const poolFactory = (Client) => {
   return class BoundPool extends Pool {
@@ -21,6 +22,7 @@ var PG = function (clientConstructor) {
   this._pools = []
   this.Connection = Connection
   this.types = require('pg-types')
+  this.DatabaseError = DatabaseError
 }
 
 if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {


Test patch:
diff --git a/packages/pg/test/integration/client/error-handling-tests.js b/packages/pg/test/integration/client/error-handling-tests.js
index 88e6d39f7..4e879c9e0 100644
--- a/packages/pg/test/integration/client/error-handling-tests.js
+++ b/packages/pg/test/integration/client/error-handling-tests.js
@@ -5,6 +5,7 @@ var util = require('util')
 
 var pg = helper.pg
 const Client = pg.Client
+const DatabaseError = pg.DatabaseError
 
 var createErorrClient = function () {
   var client = helper.client()
@@ -140,6 +141,9 @@ suite.test('when a query is binding', function (done) {
   )
 
   assert.emits(query, 'error', function (err) {
+    if (!helper.config.native) {
+      assert(err instanceof DatabaseError)
+    }
     assert.equal(err.severity, 'ERROR')
     ensureFuture(client, done)
   })
@@ -213,6 +217,9 @@ suite.test('within a simple query', (done) => {
   var query = client.query(new pg.Query("select eeeee from yodas_dsflsd where pixistix = 'zoiks!!!'"))
 
   assert.emits(query, 'error', function (error) {
+    if (!helper.config.native) {
+      assert(error instanceof DatabaseError)
+    }
     assert.equal(error.severity, 'ERROR')
     done()
   })
diff --git a/packages/pg/test/integration/client/query-error-handling-tests.js b/packages/pg/test/integration/client/query-error-handling-tests.js
index 34eab8f65..3ede5d972 100644
--- a/packages/pg/test/integration/client/query-error-handling-tests.js
+++ b/packages/pg/test/integration/client/query-error-handling-tests.js
@@ -2,6 +2,7 @@
 var helper = require('./test-helper')
 var util = require('util')
 var Query = helper.pg.Query
+var DatabaseError = helper.pg.DatabaseError
 
 test('error during query execution', function () {
   var client = new Client(helper.args)
@@ -74,6 +75,9 @@ test('9.3 column error fields', function () {
 
           client.query('CREATE TEMP TABLE column_err_test(a int NOT NULL)')
           client.query('INSERT INTO column_err_test(a) VALUES (NULL)', function (err) {
+            if (!helper.config.native) {
+              assert(err instanceof DatabaseError)
+            }
             assert.equal(err.severity, 'ERROR')
             assert.equal(err.code, '23502')
             assert.equal(err.table, 'column_err_test')
@@ -102,6 +106,9 @@ test('9.3 constraint error fields', function () {
           client.query('CREATE TEMP TABLE constraint_err_test(a int PRIMARY KEY)')
           client.query('INSERT INTO constraint_err_test(a) VALUES (1)')
           client.query('INSERT INTO constraint_err_test(a) VALUES (1)', function (err) {
+            if (!helper.config.native) {
+              assert(err instanceof DatabaseError)
+            }
             assert.equal(err.severity, 'ERROR')
             assert.equal(err.code, '23505')
             assert.equal(err.table, 'constraint_err_test')