Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions src/KuzzleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ export class KuzzleError extends Error {
*/
public status: number;
/**
* Stacktrace (only if NODE_ENV=development)
* Stacktrace
*/
public stack?: string;
public stack: string;
/**
* Kuzzle stacktrace (development mode only)
*/
public kuzzleStack?: string;
/**
* Unique ID
*/
Expand All @@ -34,11 +38,23 @@ export class KuzzleError extends Error {
*/
public count?: number;

constructor (apiError) {
constructor (apiError, stack = null) {
super(apiError.message);

this.status = apiError.status;
this.stack = apiError.stack;
if (apiError.stack) {
Reflect.defineProperty(this, 'kuzzleStack', {
value: apiError.stack
});
}

if (stack) {
const lines = stack.split('\n');
lines[0] += apiError.message;
lines[3] = ' 🡆 ' + lines[3].trimStart();
this.stack = lines.join('\n');
}

this.id = apiError.id;
this.code = apiError.code;

Expand Down
4 changes: 3 additions & 1 deletion src/protocols/abstract/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ export abstract class KuzzleAbstractProtocol extends KuzzleEventEmitter {
Discarded request: ${JSON.stringify(request)}`));
}

const stack = Error().stack;

const pending = new PendingRequest(request);
this._pendingRequests.set(request.requestId, pending);

this.once(request.requestId, response => {
this._pendingRequests.delete(request.requestId);

if (response.error) {
const error = new KuzzleError(response.error);
const error = new KuzzleError(response.error, stack);

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

Expand Down
4 changes: 2 additions & 2 deletions test/protocol/Base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('Common Protocol', () => {
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');
should(error.kuzzleStack).be.eql('you are the bug');
});
});

Expand All @@ -209,7 +209,7 @@ describe('Common Protocol', () => {
should(error).be.instanceOf(KuzzleError);
should(error.message).be.eql('foo-bar');
should(error.status).be.eql(206);
should(error.stack).be.eql('you are the bug');
should(error.kuzzleStack).be.eql('you are the bug');
should(error.errors).be.an.Array();
should(error.errors.length).eql(2);
should(error.count).eql(42);
Expand Down