Skip to content

Fix issue where error reporting wrong instanceof #543

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

Merged
merged 1 commit into from
Nov 19, 2021
Merged
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
7 changes: 0 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -149,13 +149,6 @@
}
},
"preset": "ts-jest",
"globals": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using same target as project (es5) so I can have a regression test for this issue

"ts-jest": {
"tsconfig": {
"target": "es6"
}
}
},
"globalSetup": "./tests/global-setup.ts",
"setupFilesAfterEnv": [
"./tests/setup.ts"
4 changes: 4 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { HttpError } from 'http-errors';
export class AccessTokenError extends Error {
public code: string;

/* istanbul ignore next */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor(code: string, message: string) {
super(message);

@@ -19,6 +20,7 @@ export class AccessTokenError extends Error {

// Machine readable code.
this.code = code;
Object.setPrototypeOf(this, AccessTokenError.prototype);
}
}

@@ -48,6 +50,7 @@ export class HandlerError extends Error {
public status: number | undefined;
public code: string | undefined;

/* istanbul ignore next */
constructor(error: Error | AccessTokenError | HttpError) {
super(htmlSafe(error.message));

@@ -60,5 +63,6 @@ export class HandlerError extends Error {
if ('status' in error) {
this.status = error.status;
}
Object.setPrototypeOf(this, HandlerError.prototype);
}
}
8 changes: 8 additions & 0 deletions tests/utils/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AccessTokenError, HandlerError } from '../../src/utils/errors';

describe('errors', () => {
test('should be instance of themselves', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Believe it or not, this test will fail in TS with target: es5 without the Object.setPrototypeOf(this, ThisError.prototype); workaround

expect(new AccessTokenError('code', 'message')).toBeInstanceOf(AccessTokenError);
expect(new HandlerError(new Error('message'))).toBeInstanceOf(HandlerError);
});
});