From 6ca4cadf1a0a077f7ab600eca65d297cbe47f61d Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Wed, 8 May 2024 14:25:12 +0200 Subject: [PATCH] test: add unit tests for "InternalError" --- src/core/utils/internal/devUtils.test.ts | 21 +++++++++++++++++++++ src/core/utils/internal/devUtils.ts | 11 ++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/core/utils/internal/devUtils.test.ts diff --git a/src/core/utils/internal/devUtils.test.ts b/src/core/utils/internal/devUtils.test.ts new file mode 100644 index 000000000..76dd20192 --- /dev/null +++ b/src/core/utils/internal/devUtils.test.ts @@ -0,0 +1,21 @@ +import { InternalError } from './devUtils' + +describe(InternalError, () => { + it('creates an InternalError instance', () => { + const error = new InternalError('Message') + + expect(error.name).toBe('InternalError') + expect(error.message).toBe('Message') + expect(error.toString()).toBe('InternalError: Message') + expect(error.stack).toMatch(/\w+/) + }) + + it('passes the identity check', () => { + const error = new InternalError('Message') + expect(error instanceof InternalError).toBe(true) + expect(error instanceof Error).toBe(true) + + const extraneousError = new Error('Message') + expect(extraneousError).not.toBeInstanceOf(InternalError) + }) +}) diff --git a/src/core/utils/internal/devUtils.ts b/src/core/utils/internal/devUtils.ts index e39b69264..08e5005e3 100644 --- a/src/core/utils/internal/devUtils.ts +++ b/src/core/utils/internal/devUtils.ts @@ -30,6 +30,15 @@ export const devUtils = { error, } +/** + * Internal error instance. + * Used to differentiate the library errors that must be forwarded + * to the user from the unhandled exceptions. Use this if you don't + * wish for the error to be coerced to a 500 fallback response. + */ export class InternalError extends Error { - // + constructor(message: string) { + super(message) + this.name = 'InternalError' + } }