Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 92d584f

Browse files
committed
Catch errors thrown by type parsers
1 parent 8c60a07 commit 92d584f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ Client.prototype.end = function(cb) {
7575
if(cb) setImmediate(cb);
7676
};
7777

78-
Client.prototype._readError = function(message) {
78+
Client.prototype._readError = function(err) {
7979
this._stopReading();
80-
var err = new Error(message || this.pq.errorMessage());
80+
if(!(err instanceof Error)) {
81+
err = new Error(err || this.pq.errorMessage());
82+
}
8183
this.emit('error', err);
8284
};
8385

@@ -109,7 +111,11 @@ Client.prototype._read = function() {
109111
var rows = []
110112
while(pq.getResult()) {
111113
if(pq.resultStatus() == 'PGRES_TUPLES_OK') {
112-
this._parseResults(this.pq, rows);
114+
try {
115+
this._parseResults(this.pq, rows);
116+
} catch (err) {
117+
return this._readError(err);
118+
}
113119
}
114120
if(pq.resultStatus() == 'PGRES_COPY_OUT') break;
115121
}

test/query-async.js

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Client = require('../');
22
var assert = require('assert');
33
var async = require('async');
44
var ok = require('okay');
5+
var PgTypes = require('pg-types');
56

67
describe('async query', function() {
78
before(function(done) {
@@ -14,6 +15,7 @@ describe('async query', function() {
1415

1516
after(function(done) {
1617
this.client.end(done);
18+
PgTypes.setTypeParser(23, 'text', null);
1719
});
1820

1921
it('simple query works', function(done) {
@@ -86,4 +88,18 @@ describe('async query', function() {
8688
done();
8789
});
8890
});
91+
92+
it('returns an error if there was a error parsingRows', function(done) {
93+
PgTypes.setTypeParser(23, 'text', function () {
94+
throw new Error("Type Parser Error")
95+
});
96+
var runErrorQuery = function(n, done) {
97+
this.client.query('SELECT 1', function(err) {
98+
assert(err instanceof Error, 'Should return an error instance');
99+
PgTypes.setTypeParser(23, 'text', null);
100+
done();
101+
});
102+
}.bind(this);
103+
async.timesSeries(3, runErrorQuery, done);
104+
});
89105
});

0 commit comments

Comments
 (0)