Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 12 additions & 0 deletions src/KuzzleError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

class KuzzleError extends Error {
constructor (apiError) {
super(apiError.message);

this.status = apiError.status;
this.stack = apiError.stack;
}
}

module.exports = KuzzleError;
7 changes: 3 additions & 4 deletions src/protocols/abstract/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const
KuzzleError = require('../../KuzzleError'),
uuidv4 = require('../../uuidv4'),
KuzzleEventEmitter = require('../../eventEmitter');

Expand Down Expand Up @@ -83,10 +84,8 @@ Discarded request: ${JSON.stringify(request)}`));
return new Promise((resolve, reject) => {
this.once(request.requestId, response => {
if (response.error) {
const error = new Error(response.error.message);
Object.assign(error, response.error);
error.status = response.status;
response.error = error;
const error = new KuzzleError(response.error);

this.emit('queryError', error, request);

if (request.action !== 'logout' && error.message === 'Token expired') {
Expand Down
23 changes: 22 additions & 1 deletion test/protocol/query.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var
const
should = require('should'),
sinon = require('sinon'),
KuzzleError = require('../../src/KuzzleError'),
AbstractWrapper = require('../../src/protocols/abstract/common');

describe('Protocol query management', () => {
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('Protocol query management', () => {
.then(() => Promise.reject({message: 'No error'}))
.catch(error => {
should(eventStub).be.calledOnce();
should(error).be.instanceOf(KuzzleError);
should(error.message).be.exactly('foo-bar');
});
});
Expand Down Expand Up @@ -103,5 +105,24 @@ describe('Protocol query management', () => {
should(res.status).be.exactly(42);
});
});

it('should throw a KuzzleError on error', () => {
const response = {
error: {
message: 'foo-bar',
status: 442,
stack: 'you are the bug'
}
};

return protocol.query({ requestId: 'foobar', response: response })
.then(() => Promise.reject({message: 'No error'}))
.catch(error => {
should(error).be.instanceOf(KuzzleError);
should(error.message).be.eql('foo-bar');
should(error.status).be.eql(442);
should(error.stack).be.eql('you are the bug');
});
});
});
});