Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(common): Add error chaining support to http exception #9705

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/common/exceptions/http.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ export class HttpException extends Error {
super();
this.initMessage();
this.initName();
this.initCause();
}

public cause: Error | undefined;

/**
* 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) {
this.cause = this.response;
}
}

public initMessage() {
Expand Down
11 changes: 11 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,15 @@ 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}`);
const { cause } = error;

expect(cause).to.be.eql(message);
});
});
});