From ade00810a2be23c04a5df83362970464b2dc0dc1 Mon Sep 17 00:00:00 2001 From: Alexey Vykhrystyuk Date: Tue, 11 May 2021 12:30:51 +0300 Subject: [PATCH] refactor: remove redundant stackTrace capturing + add innerError as prop --- .eslintrc.js | 2 +- src/interfaces/di-error.spec.ts | 10 ++-------- src/interfaces/di-error.ts | 11 +---------- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8a9c97c..5f18b7e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -66,7 +66,7 @@ module.exports = { // '@typescript-eslint/explicit-function-return-type': ['error', { allowExpressions: true }], // Allow properties in ctors, as it reduces code duplicates - '@typescript-eslint/no-parameter-properties': ['error', { allows: ['private readonly'] }], + '@typescript-eslint/no-parameter-properties': ['error', { allows: ['private readonly', 'protected readonly', 'public readonly'] }], 'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }], }, diff --git a/src/interfaces/di-error.spec.ts b/src/interfaces/di-error.spec.ts index 386fd75..14f558c 100644 --- a/src/interfaces/di-error.spec.ts +++ b/src/interfaces/di-error.spec.ts @@ -13,7 +13,7 @@ describe('DependencyInjectionError', () => { it('`.toString()` displays name and the message for a direct call', () => { const error = new DependencyInjectionError('Error reason message'); - assert.equal(error.toString(), 'DependencyInjectionError: Error reason message'); + assert.strictEqual(error.toString(), 'DependencyInjectionError: Error reason message'); }); // eslint-disable-next-line prefer-arrow-callback @@ -24,12 +24,6 @@ describe('DependencyInjectionError', () => { const stackTraceLines = stackTrace.split('\n'); assert.ok(stackTrace.startsWith('DependencyInjectionError: Error reason message')); assert.ok(stackTraceLines.length > 5); - assert.equal(stackTraceLines[1].indexOf('stackTraceTestCase'), 15); - }); - - it('Does not have enumerable properties - otherwise they will be displayed in `console.log(err)`', () => { - const error = new DependencyInjectionError('Error reason message'); - - assert.equal(Object.keys(error).length, 0); + assert.strictEqual(stackTraceLines[1].indexOf('stackTraceTestCase'), 15); }); }); diff --git a/src/interfaces/di-error.ts b/src/interfaces/di-error.ts index e2283fc..cdde526 100644 --- a/src/interfaces/di-error.ts +++ b/src/interfaces/di-error.ts @@ -1,18 +1,9 @@ export class DependencyInjectionError extends Error { - public constructor(message: string, innerError?: Error) { + public constructor(message: string, public readonly innerError?: Error) { super(message); // to resist minification error name is hard coded defineProperty(this, 'name', 'DependencyInjectionError'); // this.constructor.name); - - defineProperty(this, 'innerError', innerError); - - // Maintains proper stack trace for where our error was thrown (only available on V8) - if (typeof Error.captureStackTrace === 'function') { - Error.captureStackTrace(this, this.constructor); - } else { - defineProperty(this, 'stack', new Error(message).stack); - } } }