Skip to content
Merged
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": "@power-rent/try-catch",
"version": "0.0.4",
"version": "0.0.5",
"description": "A TypeScript utility for simplified async error handling with Sentry integration",
"main": "dist/index.js",
"module": "dist/esm/index.js",
Expand Down
35 changes: 31 additions & 4 deletions src/__tests__/Try.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ async function throwingFunction(_params: Record<string, unknown>): Promise<{ ok:
throw new Error('boom');
}

class GraphQLError extends Error {
name = 'GraphQLError'
}

async function throwingCustomError(_params: Record<string, unknown>): Promise<{ ok: boolean; }> {
silenceConsoleError();
throw new GraphQLError('validation error');
}

async function successfulFunction(params: Record<string, unknown>): Promise<{ ok: boolean; }> {
return { ok: true, ...params };
}
Expand All @@ -40,6 +49,7 @@ class TestClass {
describe('Try', () => {
afterEach(() => {
vi.restoreAllMocks();
Try.throwThroughErrorTypes([]);
});

it('should return default value', async () => {
Expand Down Expand Up @@ -148,30 +158,34 @@ describe('Try', () => {

it('should return the function result', async () => {
const params = { parameterKey: 'alpha' };

const result = await new Try(successfulFunction, params).unwrap();

expect(result).toEqual({ ok: true, ...params });
// captureException should not be called on success
expect(Sentry.captureException).not.toHaveBeenCalled();
});

it('should return a class method result', async () => {
const greeting = 'Hi!';
const newTest = new TestClass('newTest');

const result = await new Try(newTest.greet.bind(newTest), { greeting }).unwrap();

expect(result).toEqual('Hi!, I\'m newTest');
// captureException should not be called on success
expect(Sentry.captureException).not.toHaveBeenCalled();
});

it('should add tags', async () => {
const params = { parameterKey: 'alpha' };

const exec = new Try(throwingFunction, params)
.report('failed')
.tag('name', 'value')
.tag('test', 'true')
.unwrap();

await expect(exec).rejects.toThrow('failed');
// captureException should not be called on success

expect(Sentry.captureException).toBeCalledWith(new Error('failed', {
cause: new Error('boom')
}), {
Expand All @@ -183,10 +197,23 @@ describe('Try', () => {
});
});

it('should returns the actual error', async () => {
it('should return the actual error', async () => {
const params = { parameterKey: 'alpha' };

const result = await new Try(throwingFunction, params)
.error();

expect(result).toEqual(new Error('boom'));
});

it('should return the actual error', async () => {
Try.throwThroughErrorTypes(['GraphQLError']);
const params = { parameterKey: 'alpha' };

const exec = new Try(throwingCustomError, params)
.report('failed')
.unwrap();

await expect(exec).rejects.toThrow('validation error');
});
});
Loading