From 32d1e9617bb641bb212d386c98aa054ce48bfedf Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Wed, 25 May 2022 10:34:53 -0400 Subject: [PATCH 1/2] feat(common): Add error chaining support to http exception Adds the ability to configure the cause of an error when passing an error object to http exception. - https://nodejs.org/en/blog/release/v16.9.0/#error-cause - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#rethrowing_an_error_with_a_cause - https://github.com/microsoft/TypeScript/issues/45167 --- packages/common/exceptions/http.exception.ts | 15 +++++++++++++++ .../common/test/exceptions/http.exception.spec.ts | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/common/exceptions/http.exception.ts b/packages/common/exceptions/http.exception.ts index 84ec9361b3c..7a841cf489c 100644 --- a/packages/common/exceptions/http.exception.ts +++ b/packages/common/exceptions/http.exception.ts @@ -41,6 +41,21 @@ export class HttpException extends Error { super(); this.initMessage(); this.initName(); + this.initCause(); + } + + /** + * Configures error chaining support + * + * See: + * - https://nodejs.org/en/blog/release/v16.9.0/#error-cause + * - https://github.com/microsoft/TypeScript/issues/45167 + */ + public initCause() { + if (this.response instanceof Error) { + // @ts-ignore + this.cause = this.response; + } } public initMessage() { diff --git a/packages/common/test/exceptions/http.exception.spec.ts b/packages/common/test/exceptions/http.exception.spec.ts index c9fbab7310d..c15bae1b84d 100644 --- a/packages/common/test/exceptions/http.exception.spec.ts +++ b/packages/common/test/exceptions/http.exception.spec.ts @@ -129,4 +129,16 @@ describe('HttpException', () => { }); }); }); + + describe('initCause', () => { + it('configures a cause when message is an instance of error', () => { + const message = new Error('Some Error'); + const error = new HttpException(message, 400); + expect(`${error}`).to.be.eql(`HttpException: ${message.message}`); + // @ts-ignore + const { cause } = error; + + expect(cause).to.be.eql(message); + }); + }); }); From 727b239515a5b1ec10ced026372fa397b799ba2e Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Wed, 1 Jun 2022 15:04:29 -0400 Subject: [PATCH 2/2] Add public cause attribute --- packages/common/exceptions/http.exception.ts | 3 ++- packages/common/test/exceptions/http.exception.spec.ts | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/common/exceptions/http.exception.ts b/packages/common/exceptions/http.exception.ts index 7a841cf489c..540844298e1 100644 --- a/packages/common/exceptions/http.exception.ts +++ b/packages/common/exceptions/http.exception.ts @@ -44,6 +44,8 @@ export class HttpException extends Error { this.initCause(); } + public cause: Error | undefined; + /** * Configures error chaining support * @@ -53,7 +55,6 @@ export class HttpException extends Error { */ public initCause() { if (this.response instanceof Error) { - // @ts-ignore this.cause = this.response; } } diff --git a/packages/common/test/exceptions/http.exception.spec.ts b/packages/common/test/exceptions/http.exception.spec.ts index c15bae1b84d..a880c1bfc53 100644 --- a/packages/common/test/exceptions/http.exception.spec.ts +++ b/packages/common/test/exceptions/http.exception.spec.ts @@ -135,7 +135,6 @@ describe('HttpException', () => { const message = new Error('Some Error'); const error = new HttpException(message, 400); expect(`${error}`).to.be.eql(`HttpException: ${message.message}`); - // @ts-ignore const { cause } = error; expect(cause).to.be.eql(message);