Skip to content

Commit

Permalink
Fix connection error propagation when streaming
Browse files Browse the repository at this point in the history
If stream() is used without a handler, make sure to emit connection
errors on the stream itself.
  • Loading branch information
novemberborn committed Aug 24, 2017
1 parent f8989e1 commit d907c72
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/runner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign, isArray, noop } from 'lodash'
import { assign, isArray } from 'lodash'
import Promise from 'bluebird';
import * as helpers from './helpers';

Expand Down Expand Up @@ -80,7 +80,10 @@ assign(Runner.prototype, {

const runner = this;
const stream = new PassThrough({objectMode: true});

let hasConnection = false;
const promise = Promise.using(this.ensureConnection(), function(connection) {
hasConnection = true;
runner.connection = connection;
const sql = runner.builder.toSQL()
const err = new Error('The stream may only be used with a single query statement.');
Expand All @@ -99,9 +102,13 @@ assign(Runner.prototype, {
return promise;
}

// This promise is unreachable since no handler was given, so noop any
// exceptions. Errors should be handled in the stream's 'error' event.
promise.catch(noop);
// Emit errors on the stream if the error occurred before a connection
// could be acquired.
// If the connection was acquired, assume the error occured in the client
// code and has already been emitted on the stream. Don't emit it twice.
promise.catch(function(err) {
if (!hasConnection) stream.emit('error', err);
});
return stream;
},

Expand Down

0 comments on commit d907c72

Please sign in to comment.