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

Catching and emitting errors from type parsers #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ Client.prototype.end = function(cb) {
if(cb) setImmediate(cb);
};

Client.prototype._readError = function(message) {
Client.prototype._readError = function(err) {
this._stopReading();
var err = new Error(message || this.pq.errorMessage());
if(!(err instanceof Error)) {
err = new Error(err || this.pq.errorMessage());
}
this.emit('error', err);
};

Expand Down Expand Up @@ -109,7 +111,11 @@ Client.prototype._read = function() {
var rows = []
while(pq.getResult()) {
if(pq.resultStatus() == 'PGRES_TUPLES_OK') {
this._parseResults(this.pq, rows);
try {
this._parseResults(this.pq, rows);
} catch (err) {
return this._readError(err);
}
}
if(pq.resultStatus() == 'PGRES_COPY_OUT') break;
}
Expand Down
16 changes: 16 additions & 0 deletions test/query-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Client = require('../');
var assert = require('assert');
var async = require('async');
var ok = require('okay');
var PgTypes = require('pg-types');

describe('async query', function() {
before(function(done) {
Expand All @@ -14,6 +15,7 @@ describe('async query', function() {

after(function(done) {
this.client.end(done);
PgTypes.setTypeParser(23, 'text', null);
});

it('simple query works', function(done) {
Expand Down Expand Up @@ -86,4 +88,18 @@ describe('async query', function() {
done();
});
});

it('returns an error if there was a error parsingRows', function(done) {
PgTypes.setTypeParser(23, 'text', function () {
throw new Error("Type Parser Error")
});
var runErrorQuery = function(n, done) {
this.client.query('SELECT 1', function(err) {
assert(err instanceof Error, 'Should return an error instance');
PgTypes.setTypeParser(23, 'text', null);
done();
});
}.bind(this);
async.timesSeries(3, runErrorQuery, done);
});
});