diff --git a/src/KuzzleError.ts b/src/KuzzleError.ts index b65910f2c..505227ac4 100644 --- a/src/KuzzleError.ts +++ b/src/KuzzleError.ts @@ -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 */ @@ -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; diff --git a/src/protocols/abstract/Base.ts b/src/protocols/abstract/Base.ts index d16040eba..ec60e6cc1 100644 --- a/src/protocols/abstract/Base.ts +++ b/src/protocols/abstract/Base.ts @@ -109,6 +109,8 @@ 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); @@ -116,7 +118,7 @@ Discarded request: ${JSON.stringify(request)}`)); 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); diff --git a/test/protocol/Base.test.js b/test/protocol/Base.test.js index 4955524a7..bec5d3f0b 100644 --- a/test/protocol/Base.test.js +++ b/test/protocol/Base.test.js @@ -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'); }); }); @@ -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);