Skip to content
Draft
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
67 changes: 33 additions & 34 deletions packages/effect/src/errorReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,45 @@ const SEVERITY_TO_LEVEL = new Map<unknown, SeverityLevel>([
['Trace', 'debug'],
]);

function makeSentryErrorReporter(): EffectErrorReporter.ErrorReporter {
const reported = new WeakSet<object>();
const reported = new WeakSet<object>();

return {
[TYPE_ID]: TYPE_ID,
report({ cause }: { readonly cause: Cause.Cause<unknown> }): void {
if (reported.has(cause)) {
return;
}
reported.add(cause);
/**
* One instance for every Sentry layer, so the reporters set holds it once even when the layer is provided twice.
*/
const sentryErrorReporter: EffectErrorReporter.ErrorReporter = {
[TYPE_ID]: TYPE_ID,
report({ cause }: { readonly cause: Cause.Cause<unknown> }): void {
if (reported.has(cause)) {
return;
}
reported.add(cause);

for (const reason of cause.reasons) {
if (reason._tag === 'Interrupt') {
continue;
}
for (const reason of cause.reasons) {
if (reason._tag === 'Interrupt') {
continue;
}

// The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack,
// `cause` chain and error class.
const error = reason._tag === 'Fail' ? reason.error : reason.defect;
// The raw error is captured rather than Effect's pretty-printed copy so Sentry sees the original stack,
// `cause` chain and error class.
const error = reason._tag === 'Fail' ? reason.error : reason.defect;

if (isObjectLike(error)) {
if (reported.has(error) || error[IGNORE] === true) {
continue;
}
reported.add(error);
if (isObjectLike(error)) {
if (reported.has(error) || error[IGNORE] === true) {
continue;
}

captureException(error, {
mechanism: { type: 'auto.function.effect.error_reporter', handled: false },
captureContext: {
level: (isObjectLike(error) && SEVERITY_TO_LEVEL.get(error[SEVERITY])) || 'error',
extra: isObjectLike(error) ? { ...(error[ATTRIBUTES] as Record<string, unknown> | undefined) } : undefined,
},
});
reported.add(error);
}
},
};
}

captureException(error, {
mechanism: { type: 'auto.function.effect.error_reporter', handled: false },
captureContext: {
level: (isObjectLike(error) && SEVERITY_TO_LEVEL.get(error[SEVERITY])) || 'error',
extra: isObjectLike(error) ? { ...(error[ATTRIBUTES] as Record<string, unknown> | undefined) } : undefined,
},
});
}
},
};

/**
* Registers a Sentry `ErrorReporter` for `Effect.withErrorReporting`, `ErrorReporter.report` and the
Expand All @@ -86,8 +87,6 @@ export function makeSentryErrorReporterLayer(): Layer.Layer<never> {
return Layer.empty;
}

const sentryErrorReporter = makeSentryErrorReporter();

return Layer.effect(
CurrentErrorReporters,
Effect.gen(function* () {
Expand Down
9 changes: 9 additions & 0 deletions packages/effect/test/errorReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ describe.each([[{ subSdkName: 'browser', Sentry: sentryClient }], [{ subSdkName:
}).pipe(Effect.provide(makeLayer())),
);

it.effect('reports a failure once when the Sentry layer is provided twice', () =>
Effect.gen(function* () {
yield* Effect.fail('Provided twice').pipe(Effect.withErrorReporting, Effect.exit);
yield* flush;

expect(events).toHaveLength(1);
}).pipe(Effect.provide(makeLayer()), Effect.provide(makeLayer())),
);

it.effect('keeps reporters registered before the Sentry layer', () =>
Effect.gen(function* () {
const userReporter = vi.fn();
Expand Down
Loading