Skip to content

Commit 0ffddb4

Browse files
Merge pull request #117 from kuzzleio/develop-event-queryerror
add queryError event
2 parents 9e71368 + c9c7ee0 commit 0ffddb4

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/kuzzle.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ module.exports = Kuzzle = function (host, options, cb) {
5757
jwtTokenExpired: {lastEmitted: null, listeners: []},
5858
loginAttempt: {lastEmitted: null, listeners: []},
5959
offlineQueuePush: {listeners: []},
60-
offlineQueuePop: {listeners: []}
60+
offlineQueuePop: {listeners: []},
61+
queryError: {listeners: []}
6162
}
6263
},
6364
eventTimeout: {
@@ -704,13 +705,14 @@ function emitRequest (request, cb) {
704705
self.emitEvent('jwtTokenExpired', request, cb);
705706
}
706707

708+
if (response.error) {
709+
error = new Error(response.error.message);
710+
Object.assign(error, response.error);
711+
error.status = response.status;
712+
self.emitEvent('queryError', error, request, cb);
713+
}
714+
707715
if (cb) {
708-
if (response.error) {
709-
error = new Error(response.error.message);
710-
Object.assign(error, response.error);
711-
error.status = response.status;
712-
}
713-
714716
cb(error, response);
715717
}
716718
});

test/kuzzle/query.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ describe('Query management', function () {
5353
});
5454
});
5555

56+
it('should fire a queryError if an error occurred', function (done) {
57+
var listenerQueryError = false;
58+
59+
kuzzle.network.send = () => process.nextTick(() => kuzzle.network.emit('bar', {
60+
error: {
61+
message: 'foo-bar'
62+
}
63+
}));
64+
65+
kuzzle.addListener('queryError', function() {
66+
listenerQueryError = true;
67+
});
68+
69+
this.timeout(150);
70+
71+
emitRequest.call(kuzzle, {requestId: 'bar'}, function(error) {
72+
process.nextTick(() => {
73+
should(listenerQueryError).be.exactly(true);
74+
should(error.message).be.exactly('foo-bar');
75+
done();
76+
});
77+
});
78+
});
79+
5680
it('should launch the callback once a response has been received', function (done) {
5781
var
5882
response = {result: 'foo', error: {foo: 'bar', message: 'foobar'}, status: 42},

0 commit comments

Comments
 (0)