Skip to content

Commit

Permalink
feat(common): Add error chaining support to http exception
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnymac committed May 25, 2022
1 parent e322564 commit 32d1e96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/common/exceptions/http.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions packages/common/test/exceptions/http.exception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 32d1e96

Please sign in to comment.