From 40d09d03d82e11c779ff5662eebeecd742b71354 Mon Sep 17 00:00:00 2001 From: Aschen Date: Wed, 12 Aug 2020 13:03:00 +0200 Subject: [PATCH 1/4] WIP --- src/KuzzleError.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/KuzzleError.ts b/src/KuzzleError.ts index b65910f2c..3c888aac0 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) + * Kuzzle stacktrace (only if NODE_ENV=development) */ - public stack?: string; + public backendStack?: string; + /** + * Stacktrace + */ + public stack: string; /** * Unique ID */ @@ -38,7 +42,9 @@ export class KuzzleError extends Error { super(apiError.message); this.status = apiError.status; - this.stack = apiError.stack; + if (apiError.stack) { + this.backendStack = apiError.stack; + } this.id = apiError.id; this.code = apiError.code; From 9afb16ad8e56bc89e8a7db6fc2679760d82150ea Mon Sep 17 00:00:00 2001 From: Aschen Date: Wed, 12 Aug 2020 15:55:42 +0200 Subject: [PATCH 2/4] Enhance and clean stack trace --- src/KuzzleError.ts | 15 ++++++++------- src/protocols/abstract/Base.ts | 6 +++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/KuzzleError.ts b/src/KuzzleError.ts index 3c888aac0..4a862d560 100644 --- a/src/KuzzleError.ts +++ b/src/KuzzleError.ts @@ -10,10 +10,6 @@ export class KuzzleError extends Error { * Http status code */ public status: number; - /** - * Kuzzle stacktrace (only if NODE_ENV=development) - */ - public backendStack?: string; /** * Stacktrace */ @@ -38,13 +34,18 @@ export class KuzzleError extends Error { */ public count?: number; - constructor (apiError) { + constructor (apiError, stack = null) { super(apiError.message); this.status = apiError.status; - if (apiError.stack) { - this.backendStack = 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..3309d368f 100644 --- a/src/protocols/abstract/Base.ts +++ b/src/protocols/abstract/Base.ts @@ -56,6 +56,8 @@ export abstract class KuzzleAbstractProtocol extends KuzzleEventEmitter { this[opt] = options[opt]; } }); + + this._stacks = new Map(); } get host () { @@ -109,6 +111,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 +120,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); From c95407b34ec2f39a62e44409953f80ca1aacd3b2 Mon Sep 17 00:00:00 2001 From: Aschen Date: Wed, 12 Aug 2020 15:59:13 +0200 Subject: [PATCH 3/4] clean --- src/protocols/abstract/Base.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/protocols/abstract/Base.ts b/src/protocols/abstract/Base.ts index 3309d368f..ec60e6cc1 100644 --- a/src/protocols/abstract/Base.ts +++ b/src/protocols/abstract/Base.ts @@ -56,8 +56,6 @@ export abstract class KuzzleAbstractProtocol extends KuzzleEventEmitter { this[opt] = options[opt]; } }); - - this._stacks = new Map(); } get host () { From 2b52651f7c1c53cddaafe202c5bfe700ec56660d Mon Sep 17 00:00:00 2001 From: Aschen Date: Thu, 13 Aug 2020 11:44:12 +0200 Subject: [PATCH 4/4] fix tests --- src/KuzzleError.ts | 9 +++++++++ test/protocol/Base.test.js | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/KuzzleError.ts b/src/KuzzleError.ts index 4a862d560..505227ac4 100644 --- a/src/KuzzleError.ts +++ b/src/KuzzleError.ts @@ -14,6 +14,10 @@ export class KuzzleError extends Error { * Stacktrace */ public stack: string; + /** + * Kuzzle stacktrace (development mode only) + */ + public kuzzleStack?: string; /** * Unique ID */ @@ -38,6 +42,11 @@ export class KuzzleError extends Error { super(apiError.message); this.status = apiError.status; + if (apiError.stack) { + Reflect.defineProperty(this, 'kuzzleStack', { + value: apiError.stack + }); + } if (stack) { const lines = stack.split('\n'); 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);