Skip to content
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

Crash Error instances without processing them #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ruins-ts",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"main": "lib/ruins.js",
"files": [
Expand Down
18 changes: 14 additions & 4 deletions src/helpers/__tests__/crash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
});
});
});
4 changes: 4 additions & 0 deletions src/helpers/crash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down