From f7548237654c8db02d28630d4de5fb7108d5883e Mon Sep 17 00:00:00 2001 From: Konrad Markus Date: Thu, 15 Sep 2022 17:30:40 +0300 Subject: [PATCH 1/2] Allow crash helper to pass-through Error instances --- src/helpers/__tests__/crash.ts | 18 ++++++++++++++---- src/helpers/crash.ts | 4 ++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/helpers/__tests__/crash.ts b/src/helpers/__tests__/crash.ts index 35fc81a..de9f595 100644 --- a/src/helpers/__tests__/crash.ts +++ b/src/helpers/__tests__/crash.ts @@ -3,12 +3,16 @@ import { crash, crashMessage, crashObject } from '../crash'; describe('crash library', () => { const exampleJSON: unknown = { foo: 123 }; const exampleCyclic: unknown = {}; + const exampleError: Error = new Error('Boom!'); + // eslint-disable-next-line (exampleCyclic as any).foo = exampleCyclic; const exampleCrashMessage = crashMessage(exampleJSON); - const exampleCrashObject = crashObject(exampleJSON); - const exampleCrash = crash(exampleJSON); + const exampleCrashObject1 = crashObject(exampleJSON); + const exampleCrashObject2 = crashObject(exampleError); + const exampleCrash1 = crash(exampleJSON); + const exampleCrash2 = crash(exampleError); describe('crashMessage', () => { it('should return input as JSON if possible', () => { @@ -21,13 +25,19 @@ describe('crash library', () => { describe('crashObject', () => { it('should return an error object', () => { - expect(exampleCrashObject).toEqual(new Error(crashMessage(exampleJSON))); + expect(exampleCrashObject1).toEqual(new Error(crashMessage(exampleJSON))); + }); + it('should return an error object unchanged', () => { + expect(exampleCrashObject2).toEqual(exampleError); }); }); describe('crash', () => { it('should crash', () => { - expect(exampleCrash).toThrowError(exampleCrashObject); + expect(exampleCrash1).toThrowError(exampleCrashObject1); + }); + it('should crash', () => { + expect(exampleCrash2).toThrowError(exampleCrashObject2); }); }); }); diff --git a/src/helpers/crash.ts b/src/helpers/crash.ts index ddf1486..cd78018 100644 --- a/src/helpers/crash.ts +++ b/src/helpers/crash.ts @@ -8,7 +8,11 @@ export const crashMessage = (originalError: unknown): string => Json_.stringify(originalError), Either_.getOrElse(() => String(originalError)), ); + export const crashObject = (error: unknown): Error => { + if (error instanceof Error) { + return error; + } const errorMessage = crashMessage(error); return new Error(errorMessage); }; From 85bdc583a0c93b65655b8ed5f272abda4a64ea83 Mon Sep 17 00:00:00 2001 From: Konrad Markus Date: Thu, 15 Sep 2022 17:45:50 +0300 Subject: [PATCH 2/2] Bump version to 0.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7a05b99..ba277d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ruins-ts", - "version": "0.0.4", + "version": "0.0.5", "description": "", "main": "lib/ruins.js", "files": [