Skip to content

Commit

Permalink
Merge pull request #9705 from vinnymac/feature/error-chaining
Browse files Browse the repository at this point in the history
feat(common): Add error chaining support to http exception
  • Loading branch information
kamilmysliwiec committed Jun 15, 2022
2 parents 7501e3f + 727b239 commit 29b2ffa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/common/exceptions/http.exception.ts
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
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);
});
});
});

0 comments on commit 29b2ffa

Please sign in to comment.