From 6c87fe401ecba868feda1ffa530082c7c539321a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=C2=A0F=2E=20Romaniello?= Date: Thu, 19 May 2022 12:28:58 -0300 Subject: [PATCH] fix instaceof comparison for UnauthorizedError. closes #292 --- src/errors/UnauthorizedError.ts | 1 + test/UnauthorizedError.test.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/UnauthorizedError.test.ts diff --git a/src/errors/UnauthorizedError.ts b/src/errors/UnauthorizedError.ts index 64550e5a..f4f0bc96 100644 --- a/src/errors/UnauthorizedError.ts +++ b/src/errors/UnauthorizedError.ts @@ -13,6 +13,7 @@ export class UnauthorizedError extends Error { constructor(code: ErrorCode, error: ErrorLike) { super(error.message); + Object.setPrototypeOf(this, UnauthorizedError.prototype); this.code = code; this.status = 401; this.name = 'UnauthorizedError'; diff --git a/test/UnauthorizedError.test.ts b/test/UnauthorizedError.test.ts new file mode 100644 index 00000000..d6952131 --- /dev/null +++ b/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'); + }); +}); +