Skip to content

Commit 0273820

Browse files
committed
fix: throwThroughErrorTypes skips Sentry capture (HI-02)
Errors matching Try.throwThroughErrorTypes are now skipped from Reporter.report() (Sentry.captureException) in addition to being thrown unwrapped. Breadcrumbs still recorded. Tests in Try.test.ts assert captureException not called for both sync and async throw-through paths.
1 parent d6ca961 commit 0273820

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@power-rent/try-catch': patch
3+
---
4+
5+
Fix HI-02: errors matching `Try.throwThroughErrorTypes` are no longer reported to Sentry when `.report()` is set. Previously the error was sent to `Sentry.captureException` before being re-thrown unwrapped, contradicting the "throw-through" name. Breadcrumbs configured via `.breadcrumbs()` are still recorded.

src/__tests__/Try.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ describe('Try', () => {
406406
.unwrap();
407407

408408
await expect(exec).rejects.toThrow('validation error');
409+
expect(Sentry.captureException).not.toHaveBeenCalled();
409410
});
410411

411412
it('should not give typescript error', async () => {
@@ -1242,6 +1243,7 @@ describe('Try', () => {
12421243
.report('failed')
12431244
.unwrap();
12441245
}).toThrow('validation error');
1246+
expect(Sentry.captureException).not.toHaveBeenCalled();
12451247
});
12461248

12471249
it('should not give typescript error', () => {

src/core/Try.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,22 @@ export class Try<
185185
}
186186

187187
/**
188-
* Configure error types that should be thrown through without being wrapped.
189-
* When using `.report()`, errors matching these types will be re-thrown as-is
190-
* instead of being wrapped with the custom message.
188+
* Configure error types that should be thrown through without being wrapped
189+
* AND without being reported to the configured Reporter (Sentry).
190+
* When using `.report()`, errors matching these types are re-thrown as-is
191+
* and `Reporter.report()` is NOT called for them. Breadcrumbs configured via
192+
* `.breadcrumbs()` are still recorded.
191193
*
192194
* @param ignoreErrorTypes Array of error type names (error.name) to throw through
193195
*
194196
* @example
195197
* ```typescript
196-
* // Configure to throw ValidationError and AuthError as-is
198+
* // Configure to throw ValidationError and AuthError as-is (and skip Sentry)
197199
* Try.throwThroughErrorTypes(['ValidationError', 'AuthError']);
198200
*
199-
* // Now these errors won't be wrapped:
201+
* // Now these errors won't be wrapped or sent to Sentry:
200202
* await new Try(validateUser, userData)
201-
* .report('User validation failed') // ValidationError will be thrown as-is
203+
* .report('User validation failed') // ValidationError throws as-is, no captureException
202204
* .unwrap();
203205
* ```
204206
*/
@@ -487,18 +489,18 @@ export class Try<
487489
if (isPromiseLike<TryResult<TReturn>>(result)) {
488490
return result.then((resolved) => {
489491
if (!resolved.success) {
490-
const shouldCapture = this.config.message;
492+
const isThrowThrough = Try.ignoreErrorTypes.includes(
493+
resolved.error.name,
494+
);
495+
const shouldCapture = this.config.message && !isThrowThrough;
491496

492497
if (shouldCapture) {
493498
this.reportError(resolved.error);
494499
} else if (this.config.breadcrumbConfig) {
495500
this.addBreadcrumbsIfConfigured();
496501
}
497502

498-
if (
499-
this.config.message &&
500-
!Try.ignoreErrorTypes.includes(resolved.error.name)
501-
) {
503+
if (this.config.message && !isThrowThrough) {
502504
const wrappedError = Try.defaultReporter.createWrappedError(
503505
resolved.error,
504506
this.config.message,
@@ -513,18 +515,16 @@ export class Try<
513515
}
514516

515517
if (!result.success) {
516-
const shouldCapture = this.config.message;
518+
const isThrowThrough = Try.ignoreErrorTypes.includes(result.error.name);
519+
const shouldCapture = this.config.message && !isThrowThrough;
517520

518521
if (shouldCapture) {
519522
this.reportError(result.error);
520523
} else if (this.config.breadcrumbConfig) {
521524
this.addBreadcrumbsIfConfigured();
522525
}
523526

524-
if (
525-
this.config.message &&
526-
!Try.ignoreErrorTypes.includes(result.error.name)
527-
) {
527+
if (this.config.message && !isThrowThrough) {
528528
const wrappedError = Try.defaultReporter.createWrappedError(
529529
result.error,
530530
this.config.message,

0 commit comments

Comments
 (0)