Skip to content

Commit 8d3b552

Browse files
callmehiphopJustinBeckwith
authored andcommitted
fix: emit encoding errors on user stream (#492)
1 parent 33292d2 commit 8d3b552

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/request.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,16 @@ class DatastoreRequest {
710710
query = extend(true, new Query(), query);
711711

712712
const makeRequest = (query: Query) => {
713-
const reqOpts: RequestOptions = {
714-
query: entity.queryToQueryProto(query),
715-
};
713+
const reqOpts = {} as RequestOptions;
714+
715+
try {
716+
reqOpts.query = entity.queryToQueryProto(query);
717+
} catch (e) {
718+
// using setImmediate here to make sure this doesn't throw a
719+
// synchronous error
720+
setImmediate(onResultSet, e);
721+
return;
722+
}
716723

717724
if (options.consistency) {
718725
const code = CONSISTENCY_PROTO_CODE[options.consistency.toLowerCase()];

test/request.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,18 @@ describe('Request', () => {
819819
})
820820
.emit('reading');
821821
});
822+
823+
it('should emit an error when encoding fails', done => {
824+
const error = new Error('Encoding error.');
825+
sandbox.stub(entity, 'queryToQueryProto').throws(error);
826+
request
827+
.runQueryStream({})
828+
.on('error', (err: Error) => {
829+
assert.strictEqual(err, error);
830+
done();
831+
})
832+
.emit('reading');
833+
});
822834
});
823835

824836
describe('success', () => {

0 commit comments

Comments
 (0)