Skip to content

Commit 11c544d

Browse files
authored
✨ allow throwing original errors (#11)
Signed-off-by: w01fgang <sumin@unix-center.ru>
1 parent 966d376 commit 11c544d

3 files changed

Lines changed: 256 additions & 17 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@power-rent/try-catch",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "A TypeScript utility for simplified async error handling with Sentry integration",
55
"main": "dist/index.js",
66
"module": "dist/esm/index.js",

src/__tests__/Try.test.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ async function throwingFunction(_params: Record<string, unknown>): Promise<{ ok:
2121
throw new Error('boom');
2222
}
2323

24+
class GraphQLError extends Error {
25+
name = 'GraphQLError'
26+
}
27+
28+
async function throwingCustomError(_params: Record<string, unknown>): Promise<{ ok: boolean; }> {
29+
silenceConsoleError();
30+
throw new GraphQLError('validation error');
31+
}
32+
2433
async function successfulFunction(params: Record<string, unknown>): Promise<{ ok: boolean; }> {
2534
return { ok: true, ...params };
2635
}
@@ -40,6 +49,7 @@ class TestClass {
4049
describe('Try', () => {
4150
afterEach(() => {
4251
vi.restoreAllMocks();
52+
Try.throwThroughErrorTypes([]);
4353
});
4454

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

149159
it('should return the function result', async () => {
150160
const params = { parameterKey: 'alpha' };
161+
151162
const result = await new Try(successfulFunction, params).unwrap();
163+
152164
expect(result).toEqual({ ok: true, ...params });
153-
// captureException should not be called on success
154165
expect(Sentry.captureException).not.toHaveBeenCalled();
155166
});
156167

157168
it('should return a class method result', async () => {
158169
const greeting = 'Hi!';
159170
const newTest = new TestClass('newTest');
171+
160172
const result = await new Try(newTest.greet.bind(newTest), { greeting }).unwrap();
173+
161174
expect(result).toEqual('Hi!, I\'m newTest');
162-
// captureException should not be called on success
163175
expect(Sentry.captureException).not.toHaveBeenCalled();
164176
});
165177

166178
it('should add tags', async () => {
167179
const params = { parameterKey: 'alpha' };
180+
168181
const exec = new Try(throwingFunction, params)
169182
.report('failed')
170183
.tag('name', 'value')
171184
.tag('test', 'true')
172185
.unwrap();
186+
173187
await expect(exec).rejects.toThrow('failed');
174-
// captureException should not be called on success
188+
175189
expect(Sentry.captureException).toBeCalledWith(new Error('failed', {
176190
cause: new Error('boom')
177191
}), {
@@ -183,10 +197,23 @@ describe('Try', () => {
183197
});
184198
});
185199

186-
it('should returns the actual error', async () => {
200+
it('should return the actual error', async () => {
187201
const params = { parameterKey: 'alpha' };
202+
188203
const result = await new Try(throwingFunction, params)
189204
.error();
205+
190206
expect(result).toEqual(new Error('boom'));
191207
});
208+
209+
it('should return the actual error', async () => {
210+
Try.throwThroughErrorTypes(['GraphQLError']);
211+
const params = { parameterKey: 'alpha' };
212+
213+
const exec = new Try(throwingCustomError, params)
214+
.report('failed')
215+
.unwrap();
216+
217+
await expect(exec).rejects.toThrow('validation error');
218+
});
192219
});

0 commit comments

Comments
 (0)