Repository navigation
Expand file tree
/
Copy patherrorReporter.ts
More file actions
99 lines (87 loc) · 3.59 KB
/
Copy patherrorReporter.ts
File metadata and controls
99 lines (87 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { SeverityLevel } from '@sentry/core';
import { captureException, isObjectLike } from '@sentry/core';
import type * as Cause from 'effect/Cause';
import * as Effect from 'effect/Effect';
import type * as EffectErrorReporter from 'effect/ErrorReporter';
import * as Layer from 'effect/Layer';
import { contextExports, isEffectV4 } from './compat';
// `effect/ErrorReporter` only exists in Effect v4. Importing it fails Effect v3 builds, and reading it off the
// `effect` main entry bundles all of Effect, so the reporter uses the string keys of that API. The types make a
// changed key fail type checking.
const TYPE_ID: EffectErrorReporter.TypeId = '~effect/ErrorReporter';
const IGNORE: EffectErrorReporter.ignore = '~effect/ErrorReporter/ignore';
const SEVERITY: EffectErrorReporter.severity = '~effect/ErrorReporter/severity';
const ATTRIBUTES: EffectErrorReporter.attributes = '~effect/ErrorReporter/attributes';
/**
* Reference to the reporters that Effect calls for a reported failure. Effect looks up references by key, so it
* resolves to the same entry as `ErrorReporter.CurrentErrorReporters`. `undefined` on Effect v3.
*/
const CurrentErrorReporters = isEffectV4
? contextExports.Reference<ReadonlySet<EffectErrorReporter.ErrorReporter>>(
'effect/ErrorReporter/CurrentErrorReporters',
{ defaultValue: () => new Set() },
)
: undefined;
// Only an explicit severity annotation changes the level. Effect's own `getSeverity` falls back to `Info`, which
// would file plain failures as informational in Sentry.
const SEVERITY_TO_LEVEL = new Map<unknown, SeverityLevel>([
['Fatal', 'fatal'],
['Error', 'error'],
['Warn', 'warning'],
['Info', 'info'],
['Debug', 'debug'],
['Trace', 'debug'],
]);
function makeSentryErrorReporter(): EffectErrorReporter.ErrorReporter {
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);
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;
if (isObjectLike(error)) {
if (reported.has(error) || error[IGNORE] === true) {
continue;
}
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
* built-in HTTP and RPC reporting boundaries. Existing reporters are kept.
*
* Effect v3 has no `ErrorReporter` API, so the returned layer is empty there.
*/
export function makeSentryErrorReporterLayer(): Layer.Layer<never> {
if (!CurrentErrorReporters) {
return Layer.empty;
}
const sentryErrorReporter = makeSentryErrorReporter();
return Layer.effect(
CurrentErrorReporters,
Effect.gen(function* () {
const reporters = new Set(yield* CurrentErrorReporters);
reporters.add(sentryErrorReporter);
return reporters;
}),
);
}