Skip to content

Commit

Permalink
Handle throws in type parsers (#1218)
Browse files Browse the repository at this point in the history
* Handle throws in type parsers

* Fix throw in type parsers test for Node 0.x
  • Loading branch information
LinusU authored and brianc committed Mar 6, 2017
1 parent 4101781 commit 5cb38f5
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,19 @@ Query.prototype.handleRowDescription = function(msg) {
};

Query.prototype.handleDataRow = function(msg) {
var row = this._result.parseRow(msg.fields);
var row;

if (this._canceledDueToError) {
return;
}

try {
row = this._result.parseRow(msg.fields);
} catch (err) {
this._canceledDueToError = err;
return;
}

this.emit('row', row, this._result);
if (this._accumulateRows) {
this._result.addRow(row);
Expand Down
112 changes: 112 additions & 0 deletions test/unit/client/throw-in-type-parser-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var helper = require(__dirname + "/test-helper");
var types = require('pg-types')

test('handles throws in type parsers', function() {
var typeParserError = new Error('TEST: Throw in type parsers');

types.setTypeParser('special oid that will throw', function () {
throw typeParserError;
});

test('emits error', function() {
var handled;
var client = helper.client();
var con = client.connection;
var query = client.query('whatever');

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

assert.emits(query, 'error', function(err) {
assert.equal(err, typeParserError);
});

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");
});

test('calls callback with error', function() {
var handled;

var callbackCalled = 0;

var client = helper.client();
var con = client.connection;
var query = client.query('whatever', assert.calls(function (err) {
callbackCalled += 1;

assert.equal(callbackCalled, 1);
assert.equal(err, typeParserError);
}));

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

handled = con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled second data row message");

con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");
});

test('rejects promise with error', function() {
var handled;
var client = helper.client();
var con = client.connection;
var query = client.query('whatever');
var queryPromise = query.promise();

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

handled = con.emit('rowDescription',{
fields: [{
name: 'boom',
dataTypeID: 'special oid that will throw'
}]
});
assert.ok(handled, "should have handled row description");

handled = con.emit('dataRow', { fields: ["hi"] });
assert.ok(handled, "should have handled first data row message");

handled = con.emit('commandComplete', { text: 'INSERT 31 1' });
assert.ok(handled, "should have handled command complete");

handled = con.emit('readyForQuery');
assert.ok(handled, "should have handled ready for query");

queryPromise.catch(assert.calls(function (err) {
assert.equal(err, typeParserError);
}));
});

});

0 comments on commit 5cb38f5

Please sign in to comment.