Skip to content

Commit

Permalink
fix instaceof comparison for UnauthorizedError. closes #292
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed May 19, 2022
1 parent b1344fa commit 6c87fe4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/errors/UnauthorizedError.ts
Expand Up @@ -13,6 +13,7 @@ export class UnauthorizedError extends Error {

constructor(code: ErrorCode, error: ErrorLike) {
super(error.message);
Object.setPrototypeOf(this, UnauthorizedError.prototype);

This comment has been minimized.

Copy link
@arthurchumak

arthurchumak May 20, 2022

@jfromaniello do you know why it fails without that line?
never saw such behavior

try to test same UnauthorizedError class, works without Object.setPrototypeOf

index.js

class UnauthorizedError extends Error {
    constructor(code, error) {
      super(error.message);
      this.code = code;
      this.status = 401;
      this.name = 'UnauthorizedError';
      this.inner = error;
    }
  }

const e = new UnauthorizedError('a', new Error('b'));

console.log(e instanceof UnauthorizedError);
node index.js 
true

This comment has been minimized.

Copy link
@jfromaniello

jfromaniello May 20, 2022

Author Member

I am not sure why, it might have to do with the ES version we are compiling to. 🤔

this.code = code;
this.status = 401;
this.name = 'UnauthorizedError';
Expand Down
19 changes: 19 additions & 0 deletions test/UnauthorizedError.test.ts
@@ -0,0 +1,19 @@
import { UnauthorizedError } from '../src/errors/UnauthorizedError';
import assert from 'assert';

describe('Unauthorized Error', () => {
const e = new UnauthorizedError('credentials_bad_format', new Error('a'));

it('should be an instance of UnauthorizedError', () => {
assert.ok(e instanceof UnauthorizedError);
});

it('should contains the error code', () => {
assert.ok(e.code, 'credentials_bad_format');
});

it('should contains the error message', () => {
assert.ok(e.code, 'a');
});
});

0 comments on commit 6c87fe4

Please sign in to comment.