Skip to content

Commit

Permalink
pg: Re-export DatabaseError from 'pg-protocol'
Browse files Browse the repository at this point in the history
Before, users would have to import DatabaseError from 'pg-protocol'.  If
there are multiple versions of 'pg-protocol', you might end up using the
wrong one.

Closes #2378
  • Loading branch information
cakoose committed Jan 18, 2021
1 parent 3f3f1a7 commit 7a649d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/pg/lib/index.js
Expand Up @@ -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 {
Expand All @@ -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') {
Expand Down
7 changes: 7 additions & 0 deletions packages/pg/test/integration/client/error-handling-tests.js
Expand Up @@ -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()
Expand Down Expand Up @@ -140,6 +141,9 @@ suite.test('when a query is binding', function (done) {
)

assert.emits(query, 'error', function (err) {
if (!helper.config.native) {
assert(error instanceof DatabaseError);
}
assert.equal(err.severity, 'ERROR')
ensureFuture(client, done)
})
Expand Down Expand Up @@ -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()
})
Expand Down
Expand Up @@ -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)
Expand Down Expand Up @@ -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(error instanceof DatabaseError);
}
assert.equal(err.severity, 'ERROR')
assert.equal(err.code, '23502')
assert.equal(err.table, 'column_err_test')
Expand Down Expand Up @@ -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(error instanceof DatabaseError);
}
assert.equal(err.severity, 'ERROR')
assert.equal(err.code, '23505')
assert.equal(err.table, 'constraint_err_test')
Expand Down

0 comments on commit 7a649d3

Please sign in to comment.