@flare-engine/errors 0.4.8 → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/index.d.mts +25 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +25 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +83 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -42,6 +42,47 @@ front of the transport holds. Process-wide JS errors reach it through `installGl
|
|
|
42
42
|
past ANRs on its own; if consent must cover those, set the `io.sentry.anr.enable` meta-data to
|
|
43
43
|
`false` in your Android manifest ([Sentry Android docs](https://docs.sentry.io/platforms/android/configuration/app-not-respond/)).
|
|
44
44
|
|
|
45
|
+
Opting a dropped integration back in is a deliberate act: the `integrations` hook receives the
|
|
46
|
+
allowlist (`kept`) and the SDK's own default list (`defaults`) and returns what `init` uses. Each
|
|
47
|
+
entry is a `SentryIntegrationLike` (`{ name }`), so no `@sentry/react-native` type is needed.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createSentryTransport } from '@flare-engine/errors';
|
|
51
|
+
|
|
52
|
+
const transport = createSentryTransport({
|
|
53
|
+
dsn: 'https://xxx@o0.ingest.sentry.io/0',
|
|
54
|
+
integrations: (kept, defaults) => [
|
|
55
|
+
...kept,
|
|
56
|
+
...defaults.filter((integration) => integration.name === 'Breadcrumbs'),
|
|
57
|
+
],
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Returning `defaults`, or passing through any capture integration such as
|
|
62
|
+
`ReactNativeErrorHandlers`, re-enables Sentry's own global handlers, which report outside the
|
|
63
|
+
transport and so outside any consent check placed in front of it. A hook that throws is logged and
|
|
64
|
+
the allowlist is used.
|
|
65
|
+
|
|
66
|
+
On consent revocation, close the transport. The Sentry transport awaits the SDK's `close()` once
|
|
67
|
+
(only if it was ever loaded), and every later `report()` sends nothing; a second `close()` resolves
|
|
68
|
+
without closing again. `composeTransports` forwards `close()` to each child that has one, so the
|
|
69
|
+
chain from the first example shuts down in one call:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { composeTransports, createConsoleTransport, createSentryTransport } from '@flare-engine/errors';
|
|
73
|
+
|
|
74
|
+
const transport = composeTransports(
|
|
75
|
+
createConsoleTransport(),
|
|
76
|
+
createSentryTransport({ dsn: 'https://xxx@o0.ingest.sentry.io/0' }),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// later, when the user withdraws consent:
|
|
80
|
+
await transport.close?.();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`close` is optional on `CrashTransport`; `createNoopTransport`, `createConsoleTransport` and the
|
|
84
|
+
noop a DSN-less `createSentryTransport` returns leave it undefined.
|
|
85
|
+
|
|
45
86
|
## Catching what the boundary cannot
|
|
46
87
|
|
|
47
88
|
`ErrorBoundary` only sees errors thrown during React's render phase. Most of a game's crashes are
|
package/dist/index.d.mts
CHANGED
|
@@ -26,6 +26,12 @@ export declare function createCrashPayload(error: unknown, context?: Record<stri
|
|
|
26
26
|
*/
|
|
27
27
|
interface CrashTransport {
|
|
28
28
|
report(payload: CrashPayload): void | Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Tear the sink down, e.g. on consent revocation. Idempotent: a second call
|
|
31
|
+
* resolves without closing twice. `report()` after `close()` sends nothing.
|
|
32
|
+
* Transports with nothing to tear down leave it undefined.
|
|
33
|
+
*/
|
|
34
|
+
close?(): Promise<void>;
|
|
29
35
|
}
|
|
30
36
|
/** Transport that discards every payload. The default. */
|
|
31
37
|
export declare function createNoopTransport(): CrashTransport;
|
|
@@ -159,13 +165,31 @@ interface ErrorClientLike {
|
|
|
159
165
|
* function's own.
|
|
160
166
|
*/
|
|
161
167
|
export declare function createClientTransport(client: ErrorClientLike): CrashTransport;
|
|
168
|
+
/**
|
|
169
|
+
* The shape of a Sentry integration this package relies on. Structural, so no
|
|
170
|
+
* type from the optional `@sentry/react-native` peer leaks into the public API;
|
|
171
|
+
* the SDK's own `Integration` objects satisfy it.
|
|
172
|
+
*/
|
|
173
|
+
interface SentryIntegrationLike {
|
|
174
|
+
name: string;
|
|
175
|
+
}
|
|
162
176
|
interface SentryTransportOptions {
|
|
163
177
|
dsn?: string;
|
|
164
178
|
environment?: string;
|
|
165
179
|
release?: string;
|
|
166
180
|
sampleRate?: number;
|
|
167
181
|
enabled?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Chooses the integrations `init` receives. `kept` is the ADR-0008 allowlist
|
|
184
|
+
* (event processors only) applied to `defaults`, the SDK's own default list;
|
|
185
|
+
* without this hook `init` receives `kept`. Returning `defaults`, or any
|
|
186
|
+
* capture integration such as `ReactNativeErrorHandlers`, re-enables Sentry's
|
|
187
|
+
* own global handlers, which report outside the transport and so outside any
|
|
188
|
+
* consent check placed in front of it. A hook that throws is logged and
|
|
189
|
+
* `kept` is used.
|
|
190
|
+
*/
|
|
191
|
+
integrations?: (kept: SentryIntegrationLike[], defaults: SentryIntegrationLike[]) => SentryIntegrationLike[];
|
|
168
192
|
}
|
|
169
193
|
export declare function createSentryTransport(opts: SentryTransportOptions): CrashTransport;
|
|
170
|
-
export type { CrashPayload, CrashTransport, ErrorBoundaryProps, ErrorClientLike, ErrorUtilsLike, GlobalCaptureHandle, GlobalCaptureOptions, GlobalErrorTarget, SentryTransportOptions };
|
|
194
|
+
export type { CrashPayload, CrashTransport, ErrorBoundaryProps, ErrorClientLike, ErrorUtilsLike, GlobalCaptureHandle, GlobalCaptureOptions, GlobalErrorTarget, SentryIntegrationLike, SentryTransportOptions };
|
|
171
195
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/crash-payload.ts","../src/transport.ts","../src/compose.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"mappings":";;;;;UAIiB;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,SAAS;;;;;;wBAOK,mBACd,gBACA,UAAS,0BACR;;;;;UClBc;EACf,OAAO,SAAS,sBAAsB;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/crash-payload.ts","../src/transport.ts","../src/compose.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"mappings":";;;;;UAIiB;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,SAAS;;;;;;wBAOK,mBACd,gBACA,UAAS,0BACR;;;;;UClBc;EACf,OAAO,SAAS,sBAAsB;;;;;;EAMtC,UAAU;;;wBAyBI,uBAAuB;;;;;wBAYvB,0BAA0B;wBChD1B,qBAAqB,YAAY,mBAAmB;UCEnD;EACf,UAAU;;EAEV,WAAW,SAAS,cAAc,sBAAsB;;EAExD,YAAY;;EAEZ,UAAU;;EAEV,WAAW,gBAAgB,SAAS;;UAG5B;EACR,SAAS;;;;;;;qBAQE,sBAAsB,UAAU,oBAAoB;EAC/D,OAAO;SAEA,yBAAyB,iBAAiB;EAIjD,kBAAkB,gBAAgB,MAAM;EAYxC;EAIA,UAAU;;;;;;UCzCK;EACf,uBAAuB,gBAAgB;EACvC,iBAAiB,UAAU,gBAAgB;;;;;;;;;;UAW5B;EACf,kBAAkB,cAAc,WAAW;EAC3C,qBAAqB,cAAc,WAAW;EAC9C,aAAa;;;UAIE;;;;;;EAMf,YAAY;;;;;;EAMZ;;EAEA,UAAU;;;UAIK;;;;;EAKf;;EAEA,aAAa,WAAW;;WAEf;;;;;;;;;;;;;;;;;;;;;;;wBAqCK,0BACd,QAAQ,mBACR,UAAU,uBACT;;;;;UA8Ec;EACf,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;wBAqBnB,sBAAsB,QAAQ,kBAAkB;;;;;;UCzL/C;EACf;;UAGe;EACf;EACA;EACA;EACA;EACA;;;;;;;;;;EAUA,gBACE,MAAM,yBACN,UAAU,4BACP;;wBAuDS,sBAAsB,MAAM,yBAAyB"}
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ export declare function createCrashPayload(error: unknown, context?: Record<stri
|
|
|
26
26
|
*/
|
|
27
27
|
interface CrashTransport {
|
|
28
28
|
report(payload: CrashPayload): void | Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Tear the sink down, e.g. on consent revocation. Idempotent: a second call
|
|
31
|
+
* resolves without closing twice. `report()` after `close()` sends nothing.
|
|
32
|
+
* Transports with nothing to tear down leave it undefined.
|
|
33
|
+
*/
|
|
34
|
+
close?(): Promise<void>;
|
|
29
35
|
}
|
|
30
36
|
/** Transport that discards every payload. The default. */
|
|
31
37
|
export declare function createNoopTransport(): CrashTransport;
|
|
@@ -159,13 +165,31 @@ interface ErrorClientLike {
|
|
|
159
165
|
* function's own.
|
|
160
166
|
*/
|
|
161
167
|
export declare function createClientTransport(client: ErrorClientLike): CrashTransport;
|
|
168
|
+
/**
|
|
169
|
+
* The shape of a Sentry integration this package relies on. Structural, so no
|
|
170
|
+
* type from the optional `@sentry/react-native` peer leaks into the public API;
|
|
171
|
+
* the SDK's own `Integration` objects satisfy it.
|
|
172
|
+
*/
|
|
173
|
+
interface SentryIntegrationLike {
|
|
174
|
+
name: string;
|
|
175
|
+
}
|
|
162
176
|
interface SentryTransportOptions {
|
|
163
177
|
dsn?: string;
|
|
164
178
|
environment?: string;
|
|
165
179
|
release?: string;
|
|
166
180
|
sampleRate?: number;
|
|
167
181
|
enabled?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Chooses the integrations `init` receives. `kept` is the ADR-0008 allowlist
|
|
184
|
+
* (event processors only) applied to `defaults`, the SDK's own default list;
|
|
185
|
+
* without this hook `init` receives `kept`. Returning `defaults`, or any
|
|
186
|
+
* capture integration such as `ReactNativeErrorHandlers`, re-enables Sentry's
|
|
187
|
+
* own global handlers, which report outside the transport and so outside any
|
|
188
|
+
* consent check placed in front of it. A hook that throws is logged and
|
|
189
|
+
* `kept` is used.
|
|
190
|
+
*/
|
|
191
|
+
integrations?: (kept: SentryIntegrationLike[], defaults: SentryIntegrationLike[]) => SentryIntegrationLike[];
|
|
168
192
|
}
|
|
169
193
|
export declare function createSentryTransport(opts: SentryTransportOptions): CrashTransport;
|
|
170
|
-
export type { CrashPayload, CrashTransport, ErrorBoundaryProps, ErrorClientLike, ErrorUtilsLike, GlobalCaptureHandle, GlobalCaptureOptions, GlobalErrorTarget, SentryTransportOptions };
|
|
194
|
+
export type { CrashPayload, CrashTransport, ErrorBoundaryProps, ErrorClientLike, ErrorUtilsLike, GlobalCaptureHandle, GlobalCaptureOptions, GlobalErrorTarget, SentryIntegrationLike, SentryTransportOptions };
|
|
171
195
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/crash-payload.ts","../src/transport.ts","../src/compose.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"mappings":";;;;;UAIiB;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,SAAS;;;;;;wBAOK,mBACd,gBACA,UAAS,0BACR;;;;;UClBc;EACf,OAAO,SAAS,sBAAsB;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/crash-payload.ts","../src/transport.ts","../src/compose.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"mappings":";;;;;UAIiB;;EAEf;;EAEA;;EAEA;;EAEA;;EAEA,SAAS;;;;;;wBAOK,mBACd,gBACA,UAAS,0BACR;;;;;UClBc;EACf,OAAO,SAAS,sBAAsB;;;;;;EAMtC,UAAU;;;wBAyBI,uBAAuB;;;;;wBAYvB,0BAA0B;wBChD1B,qBAAqB,YAAY,mBAAmB;UCEnD;EACf,UAAU;;EAEV,WAAW,SAAS,cAAc,sBAAsB;;EAExD,YAAY;;EAEZ,UAAU;;EAEV,WAAW,gBAAgB,SAAS;;UAG5B;EACR,SAAS;;;;;;;qBAQE,sBAAsB,UAAU,oBAAoB;EAC/D,OAAO;SAEA,yBAAyB,iBAAiB;EAIjD,kBAAkB,gBAAgB,MAAM;EAYxC;EAIA,UAAU;;;;;;UCzCK;EACf,uBAAuB,gBAAgB;EACvC,iBAAiB,UAAU,gBAAgB;;;;;;;;;;UAW5B;EACf,kBAAkB,cAAc,WAAW;EAC3C,qBAAqB,cAAc,WAAW;EAC9C,aAAa;;;UAIE;;;;;;EAMf,YAAY;;;;;;EAMZ;;EAEA,UAAU;;;UAIK;;;;;EAKf;;EAEA,aAAa,WAAW;;WAEf;;;;;;;;;;;;;;;;;;;;;;;wBAqCK,0BACd,QAAQ,mBACR,UAAU,uBACT;;;;;UA8Ec;EACf,iBAAiB,gBAAgB;;;;;;;;;;;;;;;;;;;;wBAqBnB,sBAAsB,QAAQ,kBAAkB;;;;;;UCzL/C;EACf;;UAGe;EACf;EACA;EACA;EACA;EACA;;;;;;;;;;EAUA,gBACE,MAAM,yBACN,UAAU,4BACP;;wBAuDS,sBAAsB,MAAM,yBAAyB"}
|
package/dist/index.js
CHANGED
|
@@ -2,13 +2,22 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let react = require("react");
|
|
3
3
|
// src/compose.ts
|
|
4
4
|
function composeTransports(...transports) {
|
|
5
|
-
return {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
return {
|
|
6
|
+
async report(payload) {
|
|
7
|
+
for (const t of transports) try {
|
|
8
|
+
await t.report(payload);
|
|
9
|
+
} catch (err) {
|
|
10
|
+
console.error("[@flare-engine/errors] composeTransports: child transport threw", err);
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
async close() {
|
|
14
|
+
for (const t of transports) try {
|
|
15
|
+
await t.close?.();
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error("[@flare-engine/errors] composeTransports: child transport close threw", err);
|
|
18
|
+
}
|
|
10
19
|
}
|
|
11
|
-
}
|
|
20
|
+
};
|
|
12
21
|
}
|
|
13
22
|
// src/crash-payload.ts
|
|
14
23
|
/**
|
|
@@ -167,6 +176,10 @@ function installGlobalErrorCapture(target, options) {
|
|
|
167
176
|
const errorUtils = target.ErrorUtils;
|
|
168
177
|
const previousHandler = errorUtils?.getGlobalHandler?.();
|
|
169
178
|
if (errorUtils !== void 0) errorUtils.setGlobalHandler((error, isFatal) => {
|
|
179
|
+
if (!installed) {
|
|
180
|
+
previousHandler?.(error, isFatal);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
170
183
|
emit(error, {
|
|
171
184
|
source: "errorUtils",
|
|
172
185
|
isFatal: isFatal ?? false
|
|
@@ -181,7 +194,7 @@ function installGlobalErrorCapture(target, options) {
|
|
|
181
194
|
target.removeEventListener?.("error", onError);
|
|
182
195
|
target.removeEventListener?.("unhandledrejection", onRejection);
|
|
183
196
|
}
|
|
184
|
-
if (errorUtils !== void 0
|
|
197
|
+
if (errorUtils !== void 0) errorUtils.setGlobalHandler(previousHandler ?? (() => {}));
|
|
185
198
|
},
|
|
186
199
|
setTransport(next) {
|
|
187
200
|
transport = next;
|
|
@@ -236,43 +249,75 @@ const KEPT_INTEGRATIONS = /* @__PURE__ */ new Set([
|
|
|
236
249
|
function keepEventProcessors(defaults) {
|
|
237
250
|
return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));
|
|
238
251
|
}
|
|
252
|
+
function withIntegrationsHook(hook) {
|
|
253
|
+
return (defaults) => {
|
|
254
|
+
const kept = keepEventProcessors(defaults);
|
|
255
|
+
try {
|
|
256
|
+
return hook(kept, defaults);
|
|
257
|
+
} catch (err) {
|
|
258
|
+
console.error("[@flare-engine/errors] SentryTransportOptions.integrations threw — using the default allowlist", err);
|
|
259
|
+
return kept;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
async function closeSdk(mod) {
|
|
264
|
+
try {
|
|
265
|
+
await mod.close();
|
|
266
|
+
} catch (err) {
|
|
267
|
+
console.error("[@flare-engine/errors] Sentry close failed", err);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
239
270
|
function createSentryTransport(opts) {
|
|
240
271
|
if (!opts.dsn || opts.enabled === false) return createNoopTransport();
|
|
241
272
|
const dsn = opts.dsn;
|
|
273
|
+
const integrations = opts.integrations === void 0 ? keepEventProcessors : withIntegrationsHook(opts.integrations);
|
|
242
274
|
let mod = null;
|
|
243
275
|
let loadFailed = false;
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
276
|
+
let closed = false;
|
|
277
|
+
let closing = null;
|
|
278
|
+
return {
|
|
279
|
+
async report(payload) {
|
|
280
|
+
if (loadFailed || closed) return;
|
|
281
|
+
if (mod === null) try {
|
|
282
|
+
const loaded = await import("@sentry/react-native");
|
|
283
|
+
if (closed) return;
|
|
284
|
+
loaded.init({
|
|
285
|
+
dsn,
|
|
286
|
+
environment: opts.environment,
|
|
287
|
+
release: opts.release,
|
|
288
|
+
sampleRate: opts.sampleRate,
|
|
289
|
+
integrations,
|
|
290
|
+
enableAutoSessionTracking: false,
|
|
291
|
+
enableNativeCrashHandling: false,
|
|
292
|
+
enableAppHangTracking: false,
|
|
293
|
+
enableWatchdogTerminationTracking: false,
|
|
294
|
+
sendClientReports: false
|
|
295
|
+
});
|
|
296
|
+
mod = loaded;
|
|
297
|
+
} catch {
|
|
298
|
+
console.error("[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop");
|
|
299
|
+
loadFailed = true;
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
const err = Object.assign(new Error(payload.message), { name: payload.name });
|
|
304
|
+
if (payload.stack !== null) err.stack = payload.stack;
|
|
305
|
+
mod.captureException(err, { extra: {
|
|
306
|
+
...payload.context,
|
|
307
|
+
capturedAt: payload.timestamp
|
|
308
|
+
} });
|
|
309
|
+
} catch {
|
|
310
|
+
console.error("[@flare-engine/errors] Sentry captureException failed");
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
close() {
|
|
314
|
+
if (closing === null) {
|
|
315
|
+
closed = true;
|
|
316
|
+
closing = mod === null ? Promise.resolve() : closeSdk(mod);
|
|
317
|
+
}
|
|
318
|
+
return closing;
|
|
274
319
|
}
|
|
275
|
-
}
|
|
320
|
+
};
|
|
276
321
|
}
|
|
277
322
|
exports.ErrorBoundary = ErrorBoundary;
|
|
278
323
|
exports.composeTransports = composeTransports;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Component"],"sources":["../src/compose.ts","../src/crash-payload.ts","../src/transport.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"sourcesContent":["import type { CrashTransport } from './transport.js';\n\nexport function composeTransports(...transports: CrashTransport[]): CrashTransport {\n return {\n async report(payload) {\n for (const t of transports) {\n try {\n await t.report(payload);\n } catch (err) {\n console.error('[@flare-engine/errors] composeTransports: child transport threw', err);\n }\n }\n },\n };\n}\n","/**\n * Serializable crash payload. Everything here is JSON-safe so transports can\n * post it to Sentry / Crashlytics / a custom HTTP endpoint without extra work.\n */\nexport interface CrashPayload {\n /** Error message. */\n message: string;\n /** Error name (e.g., `TypeError`). */\n name: string;\n /** Captured stack trace, if available. */\n stack: string | null;\n /** Monotonic ms timestamp — `Date.now()` at capture. */\n timestamp: number;\n /** Optional free-form context supplied by the caller. */\n context: Record<string, unknown>;\n}\n\n/**\n * Capture an unknown thrown value as a serializable crash payload. Handles\n * `Error` instances, string throws, and arbitrary values.\n */\nexport function createCrashPayload(\n error: unknown,\n context: Record<string, unknown> = {},\n): CrashPayload {\n if (error instanceof Error) {\n return {\n message: error.message,\n name: error.name,\n stack: error.stack ?? null,\n timestamp: Date.now(),\n context,\n };\n }\n if (typeof error === 'string') {\n return { message: error, name: 'Error', stack: null, timestamp: Date.now(), context };\n }\n let message: string;\n try {\n message = JSON.stringify(error);\n } catch {\n message = String(error);\n }\n return { message, name: 'NonError', stack: null, timestamp: Date.now(), context };\n}\n","import type { CrashPayload } from './crash-payload.js';\n\n/**\n * Pluggable transport for reporting crashes. Implementations send the payload\n * wherever they need — Sentry, Crashlytics, an HTTP endpoint, or nowhere.\n */\nexport interface CrashTransport {\n report(payload: CrashPayload): void | Promise<void>;\n}\n\n/**\n * Call `transport.report` so that neither a synchronous throw nor a rejected\n * promise escapes. Left unhandled, a rejection surfaces on the global as an\n * `unhandledrejection` that `installGlobalErrorCapture` listens for, so each\n * failed report would trigger the next for as long as the transport is down.\n */\nexport function reportSafely(\n transport: CrashTransport,\n payload: CrashPayload,\n stage: 'reporting' | 'draining',\n): void {\n const onFailure = (err: unknown): void => {\n console.error(`[@flare-engine/errors] transport threw while ${stage}`, err);\n };\n try {\n void Promise.resolve(transport.report(payload)).catch(onFailure);\n } catch (err) {\n onFailure(err);\n }\n}\n\n/** Transport that discards every payload. The default. */\nexport function createNoopTransport(): CrashTransport {\n return {\n report() {\n /* no-op */\n },\n };\n}\n\n/**\n * Transport that logs payloads via `console.error`. Useful in development and\n * as a wrapper around another transport when debugging.\n */\nexport function createConsoleTransport(): CrashTransport {\n return {\n report(payload) {\n console.error('[@flare-engine/errors]', payload);\n },\n };\n}\n","import { Component, type ErrorInfo, type ReactNode } from 'react';\nimport { type CrashPayload, createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\nexport interface ErrorBoundaryProps {\n children: ReactNode;\n /** Rendered when an error is caught. Receives the payload + a reset callback. */\n fallback: (payload: CrashPayload, reset: () => void) => ReactNode;\n /** Transport invoked once per caught error. */\n transport?: CrashTransport;\n /** Additional context merged into every payload captured by this boundary. */\n context?: Record<string, unknown>;\n /** Called with both the raw error and the payload for custom side effects. */\n onError?: (error: unknown, payload: CrashPayload) => void;\n}\n\ninterface ErrorBoundaryState {\n payload: CrashPayload | null;\n}\n\n/**\n * React error boundary that captures render-phase errors into a structured\n * `CrashPayload`, forwards it to an optional transport, and renders the\n * caller-supplied fallback.\n */\nexport class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {\n state: ErrorBoundaryState = { payload: null };\n\n static getDerivedStateFromError(error: unknown): ErrorBoundaryState {\n return { payload: createCrashPayload(error) };\n }\n\n componentDidCatch(error: unknown, info: ErrorInfo): void {\n const context = {\n ...this.props.context,\n componentStack: info.componentStack ?? null,\n };\n const payload = createCrashPayload(error, context);\n this.setState({ payload });\n const { transport } = this.props;\n if (transport !== undefined) reportSafely(transport, payload, 'reporting');\n this.props.onError?.(error, payload);\n }\n\n reset = (): void => {\n this.setState({ payload: null });\n };\n\n render(): ReactNode {\n if (this.state.payload !== null) {\n return this.props.fallback(this.state.payload, this.reset);\n }\n return this.props.children;\n }\n}\n","import { createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\n/**\n * React Native's `global.ErrorUtils`, described structurally so this package\n * needs no React Native dependency or types.\n */\nexport interface ErrorUtilsLike {\n getGlobalHandler?(): ((error: unknown, isFatal?: boolean) => void) | undefined;\n setGlobalHandler(handler: (error: unknown, isFatal?: boolean) => void): void;\n}\n\n/**\n * What `installGlobalErrorCapture` needs from a global object.\n *\n * Both members are optional and independent, so one call handles either\n * platform: on web `globalThis` supplies `addEventListener`, on React Native it\n * supplies `ErrorUtils`, and in a test you pass a hand-built fake. Nothing here\n * references `window`, `document` or any RN type.\n */\nexport interface GlobalErrorTarget {\n addEventListener?(type: string, listener: (event: unknown) => void): void;\n removeEventListener?(type: string, listener: (event: unknown) => void): void;\n ErrorUtils?: ErrorUtilsLike;\n}\n\n/** Options for `installGlobalErrorCapture`. */\nexport interface GlobalCaptureOptions {\n /**\n * Where captured errors go. May be omitted at install and attached later with\n * `setTransport` — errors raised in between are held in the buffer below\n * rather than dropped.\n */\n transport?: CrashTransport;\n /**\n * How many pre-transport errors to retain. Default 20. The oldest is dropped\n * once full: a boot loop should not grow memory without bound, and the first\n * errors are the ones worth keeping.\n */\n bufferSize?: number;\n /** Context merged into every payload this capture produces. */\n context?: Record<string, unknown>;\n}\n\n/** Handle returned by `installGlobalErrorCapture`. */\nexport interface GlobalCaptureHandle {\n /**\n * Detach every handler this call installed and restore whatever was there\n * before. Safe to call more than once.\n */\n uninstall(): void;\n /** Attach or replace the transport, draining any buffered errors into it. */\n setTransport(transport: CrashTransport): void;\n /** How many errors are currently held awaiting a transport. */\n readonly buffered: number;\n}\n\n/** Pull the underlying error out of a structurally-typed DOM-ish event. */\nfunction unwrapEvent(event: unknown, key: 'error' | 'reason'): unknown {\n if (typeof event === 'object' && event !== null && key in event) {\n const value = (event as Record<string, unknown>)[key];\n if (value !== undefined && value !== null) return value;\n }\n // An ErrorEvent with no `.error` still carries a message worth reporting.\n if (typeof event === 'object' && event !== null && 'message' in event) {\n return (event as Record<string, unknown>).message;\n }\n return event;\n}\n\n/**\n * Route uncaught errors and unhandled rejections into a `CrashTransport`.\n *\n * `ErrorBoundary` only sees errors thrown during React's render phase. The\n * majority of a game's crashes are not there — they are in the update loop, in\n * an asset load, in a promise nobody awaited — and before this nothing carried\n * them to a transport.\n *\n * Both platforms are handled through the same call:\n *\n * ```ts\n * const capture = installGlobalErrorCapture(globalThis, { transport });\n * // ...\n * capture.uninstall();\n * ```\n *\n * The React Native handler is **chained, not replaced**: whatever handler was\n * installed before still runs, because on RN that handler is what shows the\n * red box in dev and terminates the app on a fatal. Swallowing it would convert\n * a visible crash into a silent one.\n */\nexport function installGlobalErrorCapture(\n target: GlobalErrorTarget,\n options?: GlobalCaptureOptions,\n): GlobalCaptureHandle {\n const bufferSize = Math.max(0, options?.bufferSize ?? 20);\n const context = options?.context ?? {};\n let transport: CrashTransport | null = options?.transport ?? null;\n const pending: ReturnType<typeof createCrashPayload>[] = [];\n let installed = true;\n\n const emit = (error: unknown, extra?: Record<string, unknown>): void => {\n const payload = createCrashPayload(error, extra ? { ...context, ...extra } : { ...context });\n if (transport === null) {\n if (bufferSize === 0) return;\n if (pending.length === bufferSize) pending.shift();\n pending.push(payload);\n return;\n }\n reportSafely(transport, payload, 'reporting');\n };\n\n const onError = (event: unknown): void => {\n emit(unwrapEvent(event, 'error'), { source: 'error' });\n };\n const onRejection = (event: unknown): void => {\n emit(unwrapEvent(event, 'reason'), { source: 'unhandledrejection' });\n };\n\n const canListen =\n typeof target.addEventListener === 'function' &&\n typeof target.removeEventListener === 'function';\n if (canListen) {\n target.addEventListener?.('error', onError);\n target.addEventListener?.('unhandledrejection', onRejection);\n }\n\n const errorUtils = target.ErrorUtils;\n const previousHandler = errorUtils?.getGlobalHandler?.();\n if (errorUtils !== undefined) {\n errorUtils.setGlobalHandler((error: unknown, isFatal?: boolean) => {\n emit(error, { source: 'errorUtils', isFatal: isFatal ?? false });\n // Chain, so the platform's own reporting still happens.\n previousHandler?.(error, isFatal);\n });\n }\n\n return {\n uninstall(): void {\n if (!installed) return;\n installed = false;\n if (canListen) {\n target.removeEventListener?.('error', onError);\n target.removeEventListener?.('unhandledrejection', onRejection);\n }\n if (errorUtils !== undefined && previousHandler !== undefined) {\n errorUtils.setGlobalHandler(previousHandler);\n }\n },\n setTransport(next: CrashTransport): void {\n transport = next;\n while (pending.length > 0) {\n reportSafely(next, pending.shift()!, 'draining');\n }\n },\n get buffered(): number {\n return pending.length;\n },\n };\n}\n\n/**\n * A Sentry-browser-shaped client, described structurally so this package takes\n * no dependency on `@sentry/browser` (or on any particular client).\n */\nexport interface ErrorClientLike {\n captureException(error: unknown, hint?: unknown): unknown;\n}\n\n/**\n * Transport over an injected browser error client.\n *\n * `createSentryTransport` is native-only — it dynamically imports\n * `@sentry/react-native`, which does not resolve on web. This is the web half:\n * the caller supplies an already-initialised client, so this package neither\n * imports nor initialises one.\n *\n * ```ts\n * import * as Sentry from '@sentry/browser';\n * Sentry.init({ dsn });\n * const transport = createClientTransport(Sentry);\n * ```\n *\n * The payload is rebuilt into an `Error` because that is what such clients\n * group by; the original stack is preserved rather than replaced with this\n * function's own.\n */\nexport function createClientTransport(client: ErrorClientLike): CrashTransport {\n return {\n report(payload): void {\n const error = new Error(payload.message);\n error.name = payload.name;\n if (payload.stack !== null) error.stack = payload.stack;\n try {\n client.captureException(error, { extra: payload.context });\n } catch (err) {\n console.error('[@flare-engine/errors] client.captureException threw', err);\n }\n },\n };\n}\n","import type { Integration } from '@sentry/react-native';\nimport type { CrashPayload } from '../crash-payload.js';\nimport type { CrashTransport } from '../transport.js';\nimport { createNoopTransport } from '../transport.js';\n\nexport interface SentryTransportOptions {\n dsn?: string;\n environment?: string;\n release?: string;\n sampleRate?: number;\n enabled?: boolean;\n}\n\n// Sentry must send only what `report()` hands it (ADR-0008). The SDK's default\n// integrations include its own global error/rejection handlers, which report\n// outside the transport and so outside any consent check placed in front of\n// it, plus collectors (breadcrumbs, device context) that attach data to every\n// event. Only event processors that enrich an event already in flight are kept.\n// Fail-safe: a name that does not match on some SDK major only loses that\n// enrichment; an unknown name can never add a capture path.\nconst KEPT_INTEGRATIONS: ReadonlySet<string> = new Set([\n 'RewriteFrames',\n 'Release',\n 'DebugMeta',\n 'SdkInfo',\n 'Dedupe',\n 'InboundFilters',\n 'NativeLinkedErrors',\n 'EventOrigin',\n]);\n\nfunction keepEventProcessors(defaults: Integration[]): Integration[] {\n return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));\n}\n\nexport function createSentryTransport(opts: SentryTransportOptions): CrashTransport {\n if (!opts.dsn || opts.enabled === false) {\n return createNoopTransport();\n }\n\n const dsn = opts.dsn;\n type SentryMod = typeof import('@sentry/react-native');\n let mod: SentryMod | null = null;\n let loadFailed = false;\n\n return {\n async report(payload: CrashPayload): Promise<void> {\n if (loadFailed) return;\n\n if (mod === null) {\n try {\n mod = await import('@sentry/react-native');\n mod.init({\n dsn,\n environment: opts.environment,\n release: opts.release,\n sampleRate: opts.sampleRate,\n integrations: keepEventProcessors,\n enableAutoSessionTracking: false,\n enableNativeCrashHandling: false,\n enableAppHangTracking: false,\n enableWatchdogTerminationTracking: false,\n sendClientReports: false,\n });\n } catch {\n console.error(\n '[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop',\n );\n loadFailed = true;\n return;\n }\n }\n\n try {\n const err = Object.assign(new Error(payload.message), { name: payload.name });\n if (payload.stack !== null) err.stack = payload.stack;\n mod.captureException(err, {\n extra: { ...payload.context, capturedAt: payload.timestamp },\n });\n } catch {\n console.error('[@flare-engine/errors] Sentry captureException failed');\n }\n },\n };\n}\n"],"mappings":";;;AAEA,SAAgB,kBAAkB,GAAG,YAA8C;CACjF,OAAO,EACL,MAAM,OAAO,SAAS;EACpB,KAAK,MAAM,KAAK,YACd,IAAI;GACF,MAAM,EAAE,OAAO,OAAO;EACxB,SAAS,KAAK;GACZ,QAAQ,MAAM,mEAAmE,GAAG;EACtF;CAEJ,EACF;AACF;;;;;;ACOA,SAAgB,mBACd,OACA,UAAmC,CAAC,GACtB;CACd,IAAI,iBAAiB,OACnB,OAAO;EACL,SAAS,MAAM;EACf,MAAM,MAAM;EACZ,OAAO,MAAM,SAAS;EACtB,WAAW,KAAK,IAAI;EACpB;CACF;CAEF,IAAI,OAAO,UAAU,UACnB,OAAO;EAAE,SAAS;EAAO,MAAM;EAAS,OAAO;EAAM,WAAW,KAAK,IAAI;EAAG;CAAQ;CAEtF,IAAI;CACJ,IAAI;EACF,UAAU,KAAK,UAAU,KAAK;CAChC,QAAQ;EACN,UAAU,OAAO,KAAK;CACxB;CACA,OAAO;EAAE;EA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K;IAClB,SAAS,KAAK;IACd,YAAY,KAAK;IACjB,cAAc;IACd,2BAA2B;IAC3B,2BAA2B;IAC3B,uBAAuB;IACvB,mCAAmC;IACnC,mBAAmB;GACrB,CAAC;EACH,QAAQ;GACN,QAAQ,MACN,gFACF;GACA,aAAa;GACb;EACF;EAGF,IAAI;GACF,MAAM,MAAM,OAAO,OAAO,IAAI,MAAM,QAAQ,OAAO,GAAG,EAAE,MAAM,QAAQ,KAAK,CAAC;GAC5E,IAAI,QAAQ,UAAU,MAAM,IAAI,QAAQ,QAAQ;GAChD,IAAI,iBAAiB,KAAK,EACxB,OAAO;IAAE,GAAG,QAAQ;IAAS,YAAY,QAAQ;GAAU,EAC7D,CAAC;EACH,QAAQ;GACN,QAAQ,MAAM,uDAAuD;EACvE;CACF,EACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["Component"],"sources":["../src/compose.ts","../src/crash-payload.ts","../src/transport.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"sourcesContent":["import type { CrashTransport } from './transport.js';\n\nexport function composeTransports(...transports: CrashTransport[]): CrashTransport {\n return {\n async report(payload) {\n for (const t of transports) {\n try {\n await t.report(payload);\n } catch (err) {\n console.error('[@flare-engine/errors] composeTransports: child transport threw', err);\n }\n }\n },\n async close() {\n for (const t of transports) {\n try {\n await t.close?.();\n } catch (err) {\n console.error(\n '[@flare-engine/errors] composeTransports: child transport close threw',\n err,\n );\n }\n }\n },\n };\n}\n","/**\n * Serializable crash payload. Everything here is JSON-safe so transports can\n * post it to Sentry / Crashlytics / a custom HTTP endpoint without extra work.\n */\nexport interface CrashPayload {\n /** Error message. */\n message: string;\n /** Error name (e.g., `TypeError`). */\n name: string;\n /** Captured stack trace, if available. */\n stack: string | null;\n /** Monotonic ms timestamp — `Date.now()` at capture. */\n timestamp: number;\n /** Optional free-form context supplied by the caller. */\n context: Record<string, unknown>;\n}\n\n/**\n * Capture an unknown thrown value as a serializable crash payload. Handles\n * `Error` instances, string throws, and arbitrary values.\n */\nexport function createCrashPayload(\n error: unknown,\n context: Record<string, unknown> = {},\n): CrashPayload {\n if (error instanceof Error) {\n return {\n message: error.message,\n name: error.name,\n stack: error.stack ?? null,\n timestamp: Date.now(),\n context,\n };\n }\n if (typeof error === 'string') {\n return { message: error, name: 'Error', stack: null, timestamp: Date.now(), context };\n }\n let message: string;\n try {\n message = JSON.stringify(error);\n } catch {\n message = String(error);\n }\n return { message, name: 'NonError', stack: null, timestamp: Date.now(), context };\n}\n","import type { CrashPayload } from './crash-payload.js';\n\n/**\n * Pluggable transport for reporting crashes. Implementations send the payload\n * wherever they need — Sentry, Crashlytics, an HTTP endpoint, or nowhere.\n */\nexport interface CrashTransport {\n report(payload: CrashPayload): void | Promise<void>;\n /**\n * Tear the sink down, e.g. on consent revocation. Idempotent: a second call\n * resolves without closing twice. `report()` after `close()` sends nothing.\n * Transports with nothing to tear down leave it undefined.\n */\n close?(): Promise<void>;\n}\n\n/**\n * Call `transport.report` so that neither a synchronous throw nor a rejected\n * promise escapes. Left unhandled, a rejection surfaces on the global as an\n * `unhandledrejection` that `installGlobalErrorCapture` listens for, so each\n * failed report would trigger the next for as long as the transport is down.\n */\nexport function reportSafely(\n transport: CrashTransport,\n payload: CrashPayload,\n stage: 'reporting' | 'draining',\n): void {\n const onFailure = (err: unknown): void => {\n console.error(`[@flare-engine/errors] transport threw while ${stage}`, err);\n };\n try {\n void Promise.resolve(transport.report(payload)).catch(onFailure);\n } catch (err) {\n onFailure(err);\n }\n}\n\n/** Transport that discards every payload. The default. */\nexport function createNoopTransport(): CrashTransport {\n return {\n report() {\n /* no-op */\n },\n };\n}\n\n/**\n * Transport that logs payloads via `console.error`. Useful in development and\n * as a wrapper around another transport when debugging.\n */\nexport function createConsoleTransport(): CrashTransport {\n return {\n report(payload) {\n console.error('[@flare-engine/errors]', payload);\n },\n };\n}\n","import { Component, type ErrorInfo, type ReactNode } from 'react';\nimport { type CrashPayload, createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\nexport interface ErrorBoundaryProps {\n children: ReactNode;\n /** Rendered when an error is caught. Receives the payload + a reset callback. */\n fallback: (payload: CrashPayload, reset: () => void) => ReactNode;\n /** Transport invoked once per caught error. */\n transport?: CrashTransport;\n /** Additional context merged into every payload captured by this boundary. */\n context?: Record<string, unknown>;\n /** Called with both the raw error and the payload for custom side effects. */\n onError?: (error: unknown, payload: CrashPayload) => void;\n}\n\ninterface ErrorBoundaryState {\n payload: CrashPayload | null;\n}\n\n/**\n * React error boundary that captures render-phase errors into a structured\n * `CrashPayload`, forwards it to an optional transport, and renders the\n * caller-supplied fallback.\n */\nexport class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {\n state: ErrorBoundaryState = { payload: null };\n\n static getDerivedStateFromError(error: unknown): ErrorBoundaryState {\n return { payload: createCrashPayload(error) };\n }\n\n componentDidCatch(error: unknown, info: ErrorInfo): void {\n const context = {\n ...this.props.context,\n componentStack: info.componentStack ?? null,\n };\n const payload = createCrashPayload(error, context);\n this.setState({ payload });\n const { transport } = this.props;\n if (transport !== undefined) reportSafely(transport, payload, 'reporting');\n this.props.onError?.(error, payload);\n }\n\n reset = (): void => {\n this.setState({ payload: null });\n };\n\n render(): ReactNode {\n if (this.state.payload !== null) {\n return this.props.fallback(this.state.payload, this.reset);\n }\n return this.props.children;\n }\n}\n","import { createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\n/**\n * React Native's `global.ErrorUtils`, described structurally so this package\n * needs no React Native dependency or types.\n */\nexport interface ErrorUtilsLike {\n getGlobalHandler?(): ((error: unknown, isFatal?: boolean) => void) | undefined;\n setGlobalHandler(handler: (error: unknown, isFatal?: boolean) => void): void;\n}\n\n/**\n * What `installGlobalErrorCapture` needs from a global object.\n *\n * Both members are optional and independent, so one call handles either\n * platform: on web `globalThis` supplies `addEventListener`, on React Native it\n * supplies `ErrorUtils`, and in a test you pass a hand-built fake. Nothing here\n * references `window`, `document` or any RN type.\n */\nexport interface GlobalErrorTarget {\n addEventListener?(type: string, listener: (event: unknown) => void): void;\n removeEventListener?(type: string, listener: (event: unknown) => void): void;\n ErrorUtils?: ErrorUtilsLike;\n}\n\n/** Options for `installGlobalErrorCapture`. */\nexport interface GlobalCaptureOptions {\n /**\n * Where captured errors go. May be omitted at install and attached later with\n * `setTransport` — errors raised in between are held in the buffer below\n * rather than dropped.\n */\n transport?: CrashTransport;\n /**\n * How many pre-transport errors to retain. Default 20. The oldest is dropped\n * once full: a boot loop should not grow memory without bound, and the first\n * errors are the ones worth keeping.\n */\n bufferSize?: number;\n /** Context merged into every payload this capture produces. */\n context?: Record<string, unknown>;\n}\n\n/** Handle returned by `installGlobalErrorCapture`. */\nexport interface GlobalCaptureHandle {\n /**\n * Detach every handler this call installed and restore whatever was there\n * before. Safe to call more than once.\n */\n uninstall(): void;\n /** Attach or replace the transport, draining any buffered errors into it. */\n setTransport(transport: CrashTransport): void;\n /** How many errors are currently held awaiting a transport. */\n readonly buffered: number;\n}\n\n/** Pull the underlying error out of a structurally-typed DOM-ish event. */\nfunction unwrapEvent(event: unknown, key: 'error' | 'reason'): unknown {\n if (typeof event === 'object' && event !== null && key in event) {\n const value = (event as Record<string, unknown>)[key];\n if (value !== undefined && value !== null) return value;\n }\n // An ErrorEvent with no `.error` still carries a message worth reporting.\n if (typeof event === 'object' && event !== null && 'message' in event) {\n return (event as Record<string, unknown>).message;\n }\n return event;\n}\n\n/**\n * Route uncaught errors and unhandled rejections into a `CrashTransport`.\n *\n * `ErrorBoundary` only sees errors thrown during React's render phase. The\n * majority of a game's crashes are not there — they are in the update loop, in\n * an asset load, in a promise nobody awaited — and before this nothing carried\n * them to a transport.\n *\n * Both platforms are handled through the same call:\n *\n * ```ts\n * const capture = installGlobalErrorCapture(globalThis, { transport });\n * // ...\n * capture.uninstall();\n * ```\n *\n * The React Native handler is **chained, not replaced**: whatever handler was\n * installed before still runs, because on RN that handler is what shows the\n * red box in dev and terminates the app on a fatal. Swallowing it would convert\n * a visible crash into a silent one.\n */\nexport function installGlobalErrorCapture(\n target: GlobalErrorTarget,\n options?: GlobalCaptureOptions,\n): GlobalCaptureHandle {\n const bufferSize = Math.max(0, options?.bufferSize ?? 20);\n const context = options?.context ?? {};\n let transport: CrashTransport | null = options?.transport ?? null;\n const pending: ReturnType<typeof createCrashPayload>[] = [];\n let installed = true;\n\n const emit = (error: unknown, extra?: Record<string, unknown>): void => {\n const payload = createCrashPayload(error, extra ? { ...context, ...extra } : { ...context });\n if (transport === null) {\n if (bufferSize === 0) return;\n if (pending.length === bufferSize) pending.shift();\n pending.push(payload);\n return;\n }\n reportSafely(transport, payload, 'reporting');\n };\n\n const onError = (event: unknown): void => {\n emit(unwrapEvent(event, 'error'), { source: 'error' });\n };\n const onRejection = (event: unknown): void => {\n emit(unwrapEvent(event, 'reason'), { source: 'unhandledrejection' });\n };\n\n const canListen =\n typeof target.addEventListener === 'function' &&\n typeof target.removeEventListener === 'function';\n if (canListen) {\n target.addEventListener?.('error', onError);\n target.addEventListener?.('unhandledrejection', onRejection);\n }\n\n const errorUtils = target.ErrorUtils;\n const previousHandler = errorUtils?.getGlobalHandler?.();\n if (errorUtils !== undefined) {\n errorUtils.setGlobalHandler((error: unknown, isFatal?: boolean) => {\n // Something may have re-captured a reference to this closure; after\n // uninstall it must only forward, never report.\n if (!installed) {\n previousHandler?.(error, isFatal);\n return;\n }\n emit(error, { source: 'errorUtils', isFatal: isFatal ?? false });\n // Chain, so the platform's own reporting still happens.\n previousHandler?.(error, isFatal);\n });\n }\n\n return {\n uninstall(): void {\n if (!installed) return;\n installed = false;\n if (canListen) {\n target.removeEventListener?.('error', onError);\n target.removeEventListener?.('unhandledrejection', onRejection);\n }\n if (errorUtils !== undefined) {\n // With nothing to restore, leave a no-op rather than the dead closure.\n errorUtils.setGlobalHandler(previousHandler ?? (() => {}));\n }\n },\n setTransport(next: CrashTransport): void {\n transport = next;\n while (pending.length > 0) {\n reportSafely(next, pending.shift()!, 'draining');\n }\n },\n get buffered(): number {\n return pending.length;\n },\n };\n}\n\n/**\n * A Sentry-browser-shaped client, described structurally so this package takes\n * no dependency on `@sentry/browser` (or on any particular client).\n */\nexport interface ErrorClientLike {\n captureException(error: unknown, hint?: unknown): unknown;\n}\n\n/**\n * Transport over an injected browser error client.\n *\n * `createSentryTransport` is native-only — it dynamically imports\n * `@sentry/react-native`, which does not resolve on web. This is the web half:\n * the caller supplies an already-initialised client, so this package neither\n * imports nor initialises one.\n *\n * ```ts\n * import * as Sentry from '@sentry/browser';\n * Sentry.init({ dsn });\n * const transport = createClientTransport(Sentry);\n * ```\n *\n * The payload is rebuilt into an `Error` because that is what such clients\n * group by; the original stack is preserved rather than replaced with this\n * function's own.\n */\nexport function createClientTransport(client: ErrorClientLike): CrashTransport {\n return {\n report(payload): void {\n const error = new Error(payload.message);\n error.name = payload.name;\n if (payload.stack !== null) error.stack = payload.stack;\n try {\n client.captureException(error, { extra: payload.context });\n } catch (err) {\n console.error('[@flare-engine/errors] client.captureException threw', err);\n }\n },\n };\n}\n","import type { CrashPayload } from '../crash-payload.js';\nimport type { CrashTransport } from '../transport.js';\nimport { createNoopTransport } from '../transport.js';\n\n/**\n * The shape of a Sentry integration this package relies on. Structural, so no\n * type from the optional `@sentry/react-native` peer leaks into the public API;\n * the SDK's own `Integration` objects satisfy it.\n */\nexport interface SentryIntegrationLike {\n name: string;\n}\n\nexport interface SentryTransportOptions {\n dsn?: string;\n environment?: string;\n release?: string;\n sampleRate?: number;\n enabled?: boolean;\n /**\n * Chooses the integrations `init` receives. `kept` is the ADR-0008 allowlist\n * (event processors only) applied to `defaults`, the SDK's own default list;\n * without this hook `init` receives `kept`. Returning `defaults`, or any\n * capture integration such as `ReactNativeErrorHandlers`, re-enables Sentry's\n * own global handlers, which report outside the transport and so outside any\n * consent check placed in front of it. A hook that throws is logged and\n * `kept` is used.\n */\n integrations?: (\n kept: SentryIntegrationLike[],\n defaults: SentryIntegrationLike[],\n ) => SentryIntegrationLike[];\n}\n\ntype SentryMod = typeof import('@sentry/react-native');\ntype IntegrationsFn = (defaults: SentryIntegrationLike[]) => SentryIntegrationLike[];\n\n// Sentry must send only what `report()` hands it (ADR-0008). The SDK's default\n// integrations include its own global error/rejection handlers, which report\n// outside the transport and so outside any consent check placed in front of\n// it, plus collectors (breadcrumbs, device context) that attach data to every\n// event. Only event processors that enrich an event already in flight are kept.\n// Fail-safe: a name that does not match on some SDK major only loses that\n// enrichment; an unknown name can never add a capture path.\nconst KEPT_INTEGRATIONS: ReadonlySet<string> = new Set([\n 'RewriteFrames',\n 'Release',\n 'DebugMeta',\n 'SdkInfo',\n 'Dedupe',\n 'InboundFilters',\n 'NativeLinkedErrors',\n 'EventOrigin',\n]);\n\nfunction keepEventProcessors(defaults: SentryIntegrationLike[]): SentryIntegrationLike[] {\n return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));\n}\n\n// The SDK calls this inside `init`, so a throwing hook must be caught here or\n// it would read as an import failure and degrade the transport to noop.\nfunction withIntegrationsHook(\n hook: NonNullable<SentryTransportOptions['integrations']>,\n): IntegrationsFn {\n return (defaults) => {\n const kept = keepEventProcessors(defaults);\n try {\n return hook(kept, defaults);\n } catch (err) {\n console.error(\n '[@flare-engine/errors] SentryTransportOptions.integrations threw — using the default allowlist',\n err,\n );\n return kept;\n }\n };\n}\n\nasync function closeSdk(mod: SentryMod): Promise<void> {\n try {\n await mod.close();\n } catch (err) {\n console.error('[@flare-engine/errors] Sentry close failed', err);\n }\n}\n\nexport function createSentryTransport(opts: SentryTransportOptions): CrashTransport {\n if (!opts.dsn || opts.enabled === false) {\n return createNoopTransport();\n }\n\n const dsn = opts.dsn;\n const integrations =\n opts.integrations === undefined ? keepEventProcessors : withIntegrationsHook(opts.integrations);\n let mod: SentryMod | null = null;\n let loadFailed = false;\n let closed = false;\n let closing: Promise<void> | null = null;\n\n return {\n async report(payload: CrashPayload): Promise<void> {\n if (loadFailed || closed) return;\n\n if (mod === null) {\n try {\n const loaded = await import('@sentry/react-native');\n // `close()` may have run while the import was pending; starting a\n // client now would leave one nothing can shut down.\n if (closed) return;\n loaded.init({\n dsn,\n environment: opts.environment,\n release: opts.release,\n sampleRate: opts.sampleRate,\n integrations,\n enableAutoSessionTracking: false,\n enableNativeCrashHandling: false,\n enableAppHangTracking: false,\n enableWatchdogTerminationTracking: false,\n sendClientReports: false,\n });\n mod = loaded;\n } catch {\n console.error(\n '[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop',\n );\n loadFailed = true;\n return;\n }\n }\n\n try {\n const err = Object.assign(new Error(payload.message), { name: payload.name });\n if (payload.stack !== null) err.stack = payload.stack;\n mod.captureException(err, {\n extra: { ...payload.context, capturedAt: payload.timestamp },\n });\n } catch {\n console.error('[@flare-engine/errors] Sentry captureException failed');\n }\n },\n close(): Promise<void> {\n if (closing === null) {\n closed = true;\n closing = mod === null ? Promise.resolve() : closeSdk(mod);\n }\n return closing;\n },\n };\n}\n"],"mappings":";;;AAEA,SAAgB,kBAAkB,GAAG,YAA8C;CACjF,OAAO;EACL,MAAM,OAAO,SAAS;GACpB,KAAK,MAAM,KAAK,YACd,IAAI;IACF,MAAM,EAAE,OAAO,OAAO;GACxB,SAAS,KAAK;IACZ,QAAQ,MAAM,mEAAmE,GAAG;GACtF;EAEJ;EACA,MAAM,QAAQ;GACZ,KAAK,MAAM,KAAK,YACd,IAAI;IACF,MAAM,EAAE,QAAQ;GAClB,SAAS,KAAK;IACZ,QAAQ,MACN,yEACA,GACF;GACF;EAEJ;CACF;AACF;;;;;;ACLA,SAAgB,mBACd,OACA,UAAmC,CAAC,GACtB;CACd,IAAI,iBAAiB,OACnB,OAAO;EACL,SAAS,MAAM;EACf,MAAM,MAAM;EACZ,OAAO,MAAM,SAAS;EACtB,WAAW,KAAK,IAAI;EACpB;CACF;CAEF,IAAI,OAAO,UAAU,UACnB,OAAO;EAAE,SAAS;EAAO,MAAM;EAAS,OAAO;EAAM,WAAW,KAAK,IAAI;E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AS,GAAG;GAC3D;GACA,OAAO;EACT;CACF;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import { Component } from "react";
|
|
2
2
|
// src/compose.ts
|
|
3
3
|
function composeTransports(...transports) {
|
|
4
|
-
return {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
return {
|
|
5
|
+
async report(payload) {
|
|
6
|
+
for (const t of transports) try {
|
|
7
|
+
await t.report(payload);
|
|
8
|
+
} catch (err) {
|
|
9
|
+
console.error("[@flare-engine/errors] composeTransports: child transport threw", err);
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
async close() {
|
|
13
|
+
for (const t of transports) try {
|
|
14
|
+
await t.close?.();
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error("[@flare-engine/errors] composeTransports: child transport close threw", err);
|
|
17
|
+
}
|
|
9
18
|
}
|
|
10
|
-
}
|
|
19
|
+
};
|
|
11
20
|
}
|
|
12
21
|
// src/crash-payload.ts
|
|
13
22
|
/**
|
|
@@ -166,6 +175,10 @@ function installGlobalErrorCapture(target, options) {
|
|
|
166
175
|
const errorUtils = target.ErrorUtils;
|
|
167
176
|
const previousHandler = errorUtils?.getGlobalHandler?.();
|
|
168
177
|
if (errorUtils !== void 0) errorUtils.setGlobalHandler((error, isFatal) => {
|
|
178
|
+
if (!installed) {
|
|
179
|
+
previousHandler?.(error, isFatal);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
169
182
|
emit(error, {
|
|
170
183
|
source: "errorUtils",
|
|
171
184
|
isFatal: isFatal ?? false
|
|
@@ -180,7 +193,7 @@ function installGlobalErrorCapture(target, options) {
|
|
|
180
193
|
target.removeEventListener?.("error", onError);
|
|
181
194
|
target.removeEventListener?.("unhandledrejection", onRejection);
|
|
182
195
|
}
|
|
183
|
-
if (errorUtils !== void 0
|
|
196
|
+
if (errorUtils !== void 0) errorUtils.setGlobalHandler(previousHandler ?? (() => {}));
|
|
184
197
|
},
|
|
185
198
|
setTransport(next) {
|
|
186
199
|
transport = next;
|
|
@@ -235,43 +248,75 @@ const KEPT_INTEGRATIONS = /* @__PURE__ */ new Set([
|
|
|
235
248
|
function keepEventProcessors(defaults) {
|
|
236
249
|
return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));
|
|
237
250
|
}
|
|
251
|
+
function withIntegrationsHook(hook) {
|
|
252
|
+
return (defaults) => {
|
|
253
|
+
const kept = keepEventProcessors(defaults);
|
|
254
|
+
try {
|
|
255
|
+
return hook(kept, defaults);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
console.error("[@flare-engine/errors] SentryTransportOptions.integrations threw — using the default allowlist", err);
|
|
258
|
+
return kept;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
async function closeSdk(mod) {
|
|
263
|
+
try {
|
|
264
|
+
await mod.close();
|
|
265
|
+
} catch (err) {
|
|
266
|
+
console.error("[@flare-engine/errors] Sentry close failed", err);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
238
269
|
function createSentryTransport(opts) {
|
|
239
270
|
if (!opts.dsn || opts.enabled === false) return createNoopTransport();
|
|
240
271
|
const dsn = opts.dsn;
|
|
272
|
+
const integrations = opts.integrations === void 0 ? keepEventProcessors : withIntegrationsHook(opts.integrations);
|
|
241
273
|
let mod = null;
|
|
242
274
|
let loadFailed = false;
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
275
|
+
let closed = false;
|
|
276
|
+
let closing = null;
|
|
277
|
+
return {
|
|
278
|
+
async report(payload) {
|
|
279
|
+
if (loadFailed || closed) return;
|
|
280
|
+
if (mod === null) try {
|
|
281
|
+
const loaded = await import("@sentry/react-native");
|
|
282
|
+
if (closed) return;
|
|
283
|
+
loaded.init({
|
|
284
|
+
dsn,
|
|
285
|
+
environment: opts.environment,
|
|
286
|
+
release: opts.release,
|
|
287
|
+
sampleRate: opts.sampleRate,
|
|
288
|
+
integrations,
|
|
289
|
+
enableAutoSessionTracking: false,
|
|
290
|
+
enableNativeCrashHandling: false,
|
|
291
|
+
enableAppHangTracking: false,
|
|
292
|
+
enableWatchdogTerminationTracking: false,
|
|
293
|
+
sendClientReports: false
|
|
294
|
+
});
|
|
295
|
+
mod = loaded;
|
|
296
|
+
} catch {
|
|
297
|
+
console.error("[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop");
|
|
298
|
+
loadFailed = true;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const err = Object.assign(new Error(payload.message), { name: payload.name });
|
|
303
|
+
if (payload.stack !== null) err.stack = payload.stack;
|
|
304
|
+
mod.captureException(err, { extra: {
|
|
305
|
+
...payload.context,
|
|
306
|
+
capturedAt: payload.timestamp
|
|
307
|
+
} });
|
|
308
|
+
} catch {
|
|
309
|
+
console.error("[@flare-engine/errors] Sentry captureException failed");
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
close() {
|
|
313
|
+
if (closing === null) {
|
|
314
|
+
closed = true;
|
|
315
|
+
closing = mod === null ? Promise.resolve() : closeSdk(mod);
|
|
316
|
+
}
|
|
317
|
+
return closing;
|
|
273
318
|
}
|
|
274
|
-
}
|
|
319
|
+
};
|
|
275
320
|
}
|
|
276
321
|
export { ErrorBoundary, composeTransports, createClientTransport, createConsoleTransport, createCrashPayload, createNoopTransport, createSentryTransport, installGlobalErrorCapture };
|
|
277
322
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/compose.ts","../src/crash-payload.ts","../src/transport.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"sourcesContent":["import type { CrashTransport } from './transport.js';\n\nexport function composeTransports(...transports: CrashTransport[]): CrashTransport {\n return {\n async report(payload) {\n for (const t of transports) {\n try {\n await t.report(payload);\n } catch (err) {\n console.error('[@flare-engine/errors] composeTransports: child transport threw', err);\n }\n }\n },\n };\n}\n","/**\n * Serializable crash payload. Everything here is JSON-safe so transports can\n * post it to Sentry / Crashlytics / a custom HTTP endpoint without extra work.\n */\nexport interface CrashPayload {\n /** Error message. */\n message: string;\n /** Error name (e.g., `TypeError`). */\n name: string;\n /** Captured stack trace, if available. */\n stack: string | null;\n /** Monotonic ms timestamp — `Date.now()` at capture. */\n timestamp: number;\n /** Optional free-form context supplied by the caller. */\n context: Record<string, unknown>;\n}\n\n/**\n * Capture an unknown thrown value as a serializable crash payload. Handles\n * `Error` instances, string throws, and arbitrary values.\n */\nexport function createCrashPayload(\n error: unknown,\n context: Record<string, unknown> = {},\n): CrashPayload {\n if (error instanceof Error) {\n return {\n message: error.message,\n name: error.name,\n stack: error.stack ?? null,\n timestamp: Date.now(),\n context,\n };\n }\n if (typeof error === 'string') {\n return { message: error, name: 'Error', stack: null, timestamp: Date.now(), context };\n }\n let message: string;\n try {\n message = JSON.stringify(error);\n } catch {\n message = String(error);\n }\n return { message, name: 'NonError', stack: null, timestamp: Date.now(), context };\n}\n","import type { CrashPayload } from './crash-payload.js';\n\n/**\n * Pluggable transport for reporting crashes. Implementations send the payload\n * wherever they need — Sentry, Crashlytics, an HTTP endpoint, or nowhere.\n */\nexport interface CrashTransport {\n report(payload: CrashPayload): void | Promise<void>;\n}\n\n/**\n * Call `transport.report` so that neither a synchronous throw nor a rejected\n * promise escapes. Left unhandled, a rejection surfaces on the global as an\n * `unhandledrejection` that `installGlobalErrorCapture` listens for, so each\n * failed report would trigger the next for as long as the transport is down.\n */\nexport function reportSafely(\n transport: CrashTransport,\n payload: CrashPayload,\n stage: 'reporting' | 'draining',\n): void {\n const onFailure = (err: unknown): void => {\n console.error(`[@flare-engine/errors] transport threw while ${stage}`, err);\n };\n try {\n void Promise.resolve(transport.report(payload)).catch(onFailure);\n } catch (err) {\n onFailure(err);\n }\n}\n\n/** Transport that discards every payload. The default. */\nexport function createNoopTransport(): CrashTransport {\n return {\n report() {\n /* no-op */\n },\n };\n}\n\n/**\n * Transport that logs payloads via `console.error`. Useful in development and\n * as a wrapper around another transport when debugging.\n */\nexport function createConsoleTransport(): CrashTransport {\n return {\n report(payload) {\n console.error('[@flare-engine/errors]', payload);\n },\n };\n}\n","import { Component, type ErrorInfo, type ReactNode } from 'react';\nimport { type CrashPayload, createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\nexport interface ErrorBoundaryProps {\n children: ReactNode;\n /** Rendered when an error is caught. Receives the payload + a reset callback. */\n fallback: (payload: CrashPayload, reset: () => void) => ReactNode;\n /** Transport invoked once per caught error. */\n transport?: CrashTransport;\n /** Additional context merged into every payload captured by this boundary. */\n context?: Record<string, unknown>;\n /** Called with both the raw error and the payload for custom side effects. */\n onError?: (error: unknown, payload: CrashPayload) => void;\n}\n\ninterface ErrorBoundaryState {\n payload: CrashPayload | null;\n}\n\n/**\n * React error boundary that captures render-phase errors into a structured\n * `CrashPayload`, forwards it to an optional transport, and renders the\n * caller-supplied fallback.\n */\nexport class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {\n state: ErrorBoundaryState = { payload: null };\n\n static getDerivedStateFromError(error: unknown): ErrorBoundaryState {\n return { payload: createCrashPayload(error) };\n }\n\n componentDidCatch(error: unknown, info: ErrorInfo): void {\n const context = {\n ...this.props.context,\n componentStack: info.componentStack ?? null,\n };\n const payload = createCrashPayload(error, context);\n this.setState({ payload });\n const { transport } = this.props;\n if (transport !== undefined) reportSafely(transport, payload, 'reporting');\n this.props.onError?.(error, payload);\n }\n\n reset = (): void => {\n this.setState({ payload: null });\n };\n\n render(): ReactNode {\n if (this.state.payload !== null) {\n return this.props.fallback(this.state.payload, this.reset);\n }\n return this.props.children;\n }\n}\n","import { createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\n/**\n * React Native's `global.ErrorUtils`, described structurally so this package\n * needs no React Native dependency or types.\n */\nexport interface ErrorUtilsLike {\n getGlobalHandler?(): ((error: unknown, isFatal?: boolean) => void) | undefined;\n setGlobalHandler(handler: (error: unknown, isFatal?: boolean) => void): void;\n}\n\n/**\n * What `installGlobalErrorCapture` needs from a global object.\n *\n * Both members are optional and independent, so one call handles either\n * platform: on web `globalThis` supplies `addEventListener`, on React Native it\n * supplies `ErrorUtils`, and in a test you pass a hand-built fake. Nothing here\n * references `window`, `document` or any RN type.\n */\nexport interface GlobalErrorTarget {\n addEventListener?(type: string, listener: (event: unknown) => void): void;\n removeEventListener?(type: string, listener: (event: unknown) => void): void;\n ErrorUtils?: ErrorUtilsLike;\n}\n\n/** Options for `installGlobalErrorCapture`. */\nexport interface GlobalCaptureOptions {\n /**\n * Where captured errors go. May be omitted at install and attached later with\n * `setTransport` — errors raised in between are held in the buffer below\n * rather than dropped.\n */\n transport?: CrashTransport;\n /**\n * How many pre-transport errors to retain. Default 20. The oldest is dropped\n * once full: a boot loop should not grow memory without bound, and the first\n * errors are the ones worth keeping.\n */\n bufferSize?: number;\n /** Context merged into every payload this capture produces. */\n context?: Record<string, unknown>;\n}\n\n/** Handle returned by `installGlobalErrorCapture`. */\nexport interface GlobalCaptureHandle {\n /**\n * Detach every handler this call installed and restore whatever was there\n * before. Safe to call more than once.\n */\n uninstall(): void;\n /** Attach or replace the transport, draining any buffered errors into it. */\n setTransport(transport: CrashTransport): void;\n /** How many errors are currently held awaiting a transport. */\n readonly buffered: number;\n}\n\n/** Pull the underlying error out of a structurally-typed DOM-ish event. */\nfunction unwrapEvent(event: unknown, key: 'error' | 'reason'): unknown {\n if (typeof event === 'object' && event !== null && key in event) {\n const value = (event as Record<string, unknown>)[key];\n if (value !== undefined && value !== null) return value;\n }\n // An ErrorEvent with no `.error` still carries a message worth reporting.\n if (typeof event === 'object' && event !== null && 'message' in event) {\n return (event as Record<string, unknown>).message;\n }\n return event;\n}\n\n/**\n * Route uncaught errors and unhandled rejections into a `CrashTransport`.\n *\n * `ErrorBoundary` only sees errors thrown during React's render phase. The\n * majority of a game's crashes are not there — they are in the update loop, in\n * an asset load, in a promise nobody awaited — and before this nothing carried\n * them to a transport.\n *\n * Both platforms are handled through the same call:\n *\n * ```ts\n * const capture = installGlobalErrorCapture(globalThis, { transport });\n * // ...\n * capture.uninstall();\n * ```\n *\n * The React Native handler is **chained, not replaced**: whatever handler was\n * installed before still runs, because on RN that handler is what shows the\n * red box in dev and terminates the app on a fatal. Swallowing it would convert\n * a visible crash into a silent one.\n */\nexport function installGlobalErrorCapture(\n target: GlobalErrorTarget,\n options?: GlobalCaptureOptions,\n): GlobalCaptureHandle {\n const bufferSize = Math.max(0, options?.bufferSize ?? 20);\n const context = options?.context ?? {};\n let transport: CrashTransport | null = options?.transport ?? null;\n const pending: ReturnType<typeof createCrashPayload>[] = [];\n let installed = true;\n\n const emit = (error: unknown, extra?: Record<string, unknown>): void => {\n const payload = createCrashPayload(error, extra ? { ...context, ...extra } : { ...context });\n if (transport === null) {\n if (bufferSize === 0) return;\n if (pending.length === bufferSize) pending.shift();\n pending.push(payload);\n return;\n }\n reportSafely(transport, payload, 'reporting');\n };\n\n const onError = (event: unknown): void => {\n emit(unwrapEvent(event, 'error'), { source: 'error' });\n };\n const onRejection = (event: unknown): void => {\n emit(unwrapEvent(event, 'reason'), { source: 'unhandledrejection' });\n };\n\n const canListen =\n typeof target.addEventListener === 'function' &&\n typeof target.removeEventListener === 'function';\n if (canListen) {\n target.addEventListener?.('error', onError);\n target.addEventListener?.('unhandledrejection', onRejection);\n }\n\n const errorUtils = target.ErrorUtils;\n const previousHandler = errorUtils?.getGlobalHandler?.();\n if (errorUtils !== undefined) {\n errorUtils.setGlobalHandler((error: unknown, isFatal?: boolean) => {\n emit(error, { source: 'errorUtils', isFatal: isFatal ?? false });\n // Chain, so the platform's own reporting still happens.\n previousHandler?.(error, isFatal);\n });\n }\n\n return {\n uninstall(): void {\n if (!installed) return;\n installed = false;\n if (canListen) {\n target.removeEventListener?.('error', onError);\n target.removeEventListener?.('unhandledrejection', onRejection);\n }\n if (errorUtils !== undefined && previousHandler !== undefined) {\n errorUtils.setGlobalHandler(previousHandler);\n }\n },\n setTransport(next: CrashTransport): void {\n transport = next;\n while (pending.length > 0) {\n reportSafely(next, pending.shift()!, 'draining');\n }\n },\n get buffered(): number {\n return pending.length;\n },\n };\n}\n\n/**\n * A Sentry-browser-shaped client, described structurally so this package takes\n * no dependency on `@sentry/browser` (or on any particular client).\n */\nexport interface ErrorClientLike {\n captureException(error: unknown, hint?: unknown): unknown;\n}\n\n/**\n * Transport over an injected browser error client.\n *\n * `createSentryTransport` is native-only — it dynamically imports\n * `@sentry/react-native`, which does not resolve on web. This is the web half:\n * the caller supplies an already-initialised client, so this package neither\n * imports nor initialises one.\n *\n * ```ts\n * import * as Sentry from '@sentry/browser';\n * Sentry.init({ dsn });\n * const transport = createClientTransport(Sentry);\n * ```\n *\n * The payload is rebuilt into an `Error` because that is what such clients\n * group by; the original stack is preserved rather than replaced with this\n * function's own.\n */\nexport function createClientTransport(client: ErrorClientLike): CrashTransport {\n return {\n report(payload): void {\n const error = new Error(payload.message);\n error.name = payload.name;\n if (payload.stack !== null) error.stack = payload.stack;\n try {\n client.captureException(error, { extra: payload.context });\n } catch (err) {\n console.error('[@flare-engine/errors] client.captureException threw', err);\n }\n },\n };\n}\n","import type { Integration } from '@sentry/react-native';\nimport type { CrashPayload } from '../crash-payload.js';\nimport type { CrashTransport } from '../transport.js';\nimport { createNoopTransport } from '../transport.js';\n\nexport interface SentryTransportOptions {\n dsn?: string;\n environment?: string;\n release?: string;\n sampleRate?: number;\n enabled?: boolean;\n}\n\n// Sentry must send only what `report()` hands it (ADR-0008). The SDK's default\n// integrations include its own global error/rejection handlers, which report\n// outside the transport and so outside any consent check placed in front of\n// it, plus collectors (breadcrumbs, device context) that attach data to every\n// event. Only event processors that enrich an event already in flight are kept.\n// Fail-safe: a name that does not match on some SDK major only loses that\n// enrichment; an unknown name can never add a capture path.\nconst KEPT_INTEGRATIONS: ReadonlySet<string> = new Set([\n 'RewriteFrames',\n 'Release',\n 'DebugMeta',\n 'SdkInfo',\n 'Dedupe',\n 'InboundFilters',\n 'NativeLinkedErrors',\n 'EventOrigin',\n]);\n\nfunction keepEventProcessors(defaults: Integration[]): Integration[] {\n return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));\n}\n\nexport function createSentryTransport(opts: SentryTransportOptions): CrashTransport {\n if (!opts.dsn || opts.enabled === false) {\n return createNoopTransport();\n }\n\n const dsn = opts.dsn;\n type SentryMod = typeof import('@sentry/react-native');\n let mod: SentryMod | null = null;\n let loadFailed = false;\n\n return {\n async report(payload: CrashPayload): Promise<void> {\n if (loadFailed) return;\n\n if (mod === null) {\n try {\n mod = await import('@sentry/react-native');\n mod.init({\n dsn,\n environment: opts.environment,\n release: opts.release,\n sampleRate: opts.sampleRate,\n integrations: keepEventProcessors,\n enableAutoSessionTracking: false,\n enableNativeCrashHandling: false,\n enableAppHangTracking: false,\n enableWatchdogTerminationTracking: false,\n sendClientReports: false,\n });\n } catch {\n console.error(\n '[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop',\n );\n loadFailed = true;\n return;\n }\n }\n\n try {\n const err = Object.assign(new Error(payload.message), { name: payload.name });\n if (payload.stack !== null) err.stack = payload.stack;\n mod.captureException(err, {\n extra: { ...payload.context, capturedAt: payload.timestamp },\n });\n } catch {\n console.error('[@flare-engine/errors] Sentry captureException failed');\n }\n },\n };\n}\n"],"mappings":";;AAEA,SAAgB,kBAAkB,GAAG,YAA8C;CACjF,OAAO,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S,KAAK;GACZ,QAAQ,MAAM,wDAAwD,GAAG;EAC3E;CACF,EACF;AACF;;ACpLA,MAAM,oCAAyC,IAAI,IAAI;CACrD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,SAAS,oBAAoB,UAAwC;CACnE,OAAO,SAAS,QAAQ,gBAAgB,kBAAkB,IAAI,YAAY,IAAI,CAAC;AACjF;AAEA,SAAgB,sBAAsB,MAA8C;CAClF,IAAI,CAAC,KAAK,OAAO,KAAK,YAAY,OAChC,OAAO,oBAAoB;CAG7B,MAAM,MAAM,KAAK;CAEjB,IAAI,MAAwB;CAC5B,IAAI,aAAa;CAEjB,OAAO,EACL,MAAM,OAAO,SAAsC;EACjD,IAAI,YAAY;EAEhB,IAAI,QAAQ,MACV,IAAI;GACF,MAAM,MAAM,OAAO;GACnB,IAAI,KAAK;IACP;IACA,aAAa,KAAK;IAClB,SAAS,KAAK;IACd,YAAY,KAAK;IACjB,cAAc;IACd,2BAA2B;IAC3B,2BAA2B;IAC3B,uBAAuB;IACvB,mCAAmC;IACnC,mBAAmB;GACrB,CAAC;EACH,QAAQ;GACN,QAAQ,MACN,gFACF;GACA,aAAa;GACb;EACF;EAGF,IAAI;GACF,MAAM,MAAM,OAAO,OAAO,IAAI,MAAM,QAAQ,OAAO,GAAG,EAAE,MAAM,QAAQ,KAAK,CAAC;GAC5E,IAAI,QAAQ,UAAU,MAAM,IAAI,QAAQ,QAAQ;GAChD,IAAI,iBAAiB,KAAK,EACxB,OAAO;IAAE,GAAG,QAAQ;IAAS,YAAY,QAAQ;GAAU,EAC7D,CAAC;EACH,QAAQ;GACN,QAAQ,MAAM,uDAAuD;EACvE;CACF,EACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/compose.ts","../src/crash-payload.ts","../src/transport.ts","../src/error-boundary.tsx","../src/global-capture.ts","../src/transports/sentry.ts"],"sourcesContent":["import type { CrashTransport } from './transport.js';\n\nexport function composeTransports(...transports: CrashTransport[]): CrashTransport {\n return {\n async report(payload) {\n for (const t of transports) {\n try {\n await t.report(payload);\n } catch (err) {\n console.error('[@flare-engine/errors] composeTransports: child transport threw', err);\n }\n }\n },\n async close() {\n for (const t of transports) {\n try {\n await t.close?.();\n } catch (err) {\n console.error(\n '[@flare-engine/errors] composeTransports: child transport close threw',\n err,\n );\n }\n }\n },\n };\n}\n","/**\n * Serializable crash payload. Everything here is JSON-safe so transports can\n * post it to Sentry / Crashlytics / a custom HTTP endpoint without extra work.\n */\nexport interface CrashPayload {\n /** Error message. */\n message: string;\n /** Error name (e.g., `TypeError`). */\n name: string;\n /** Captured stack trace, if available. */\n stack: string | null;\n /** Monotonic ms timestamp — `Date.now()` at capture. */\n timestamp: number;\n /** Optional free-form context supplied by the caller. */\n context: Record<string, unknown>;\n}\n\n/**\n * Capture an unknown thrown value as a serializable crash payload. Handles\n * `Error` instances, string throws, and arbitrary values.\n */\nexport function createCrashPayload(\n error: unknown,\n context: Record<string, unknown> = {},\n): CrashPayload {\n if (error instanceof Error) {\n return {\n message: error.message,\n name: error.name,\n stack: error.stack ?? null,\n timestamp: Date.now(),\n context,\n };\n }\n if (typeof error === 'string') {\n return { message: error, name: 'Error', stack: null, timestamp: Date.now(), context };\n }\n let message: string;\n try {\n message = JSON.stringify(error);\n } catch {\n message = String(error);\n }\n return { message, name: 'NonError', stack: null, timestamp: Date.now(), context };\n}\n","import type { CrashPayload } from './crash-payload.js';\n\n/**\n * Pluggable transport for reporting crashes. Implementations send the payload\n * wherever they need — Sentry, Crashlytics, an HTTP endpoint, or nowhere.\n */\nexport interface CrashTransport {\n report(payload: CrashPayload): void | Promise<void>;\n /**\n * Tear the sink down, e.g. on consent revocation. Idempotent: a second call\n * resolves without closing twice. `report()` after `close()` sends nothing.\n * Transports with nothing to tear down leave it undefined.\n */\n close?(): Promise<void>;\n}\n\n/**\n * Call `transport.report` so that neither a synchronous throw nor a rejected\n * promise escapes. Left unhandled, a rejection surfaces on the global as an\n * `unhandledrejection` that `installGlobalErrorCapture` listens for, so each\n * failed report would trigger the next for as long as the transport is down.\n */\nexport function reportSafely(\n transport: CrashTransport,\n payload: CrashPayload,\n stage: 'reporting' | 'draining',\n): void {\n const onFailure = (err: unknown): void => {\n console.error(`[@flare-engine/errors] transport threw while ${stage}`, err);\n };\n try {\n void Promise.resolve(transport.report(payload)).catch(onFailure);\n } catch (err) {\n onFailure(err);\n }\n}\n\n/** Transport that discards every payload. The default. */\nexport function createNoopTransport(): CrashTransport {\n return {\n report() {\n /* no-op */\n },\n };\n}\n\n/**\n * Transport that logs payloads via `console.error`. Useful in development and\n * as a wrapper around another transport when debugging.\n */\nexport function createConsoleTransport(): CrashTransport {\n return {\n report(payload) {\n console.error('[@flare-engine/errors]', payload);\n },\n };\n}\n","import { Component, type ErrorInfo, type ReactNode } from 'react';\nimport { type CrashPayload, createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\nexport interface ErrorBoundaryProps {\n children: ReactNode;\n /** Rendered when an error is caught. Receives the payload + a reset callback. */\n fallback: (payload: CrashPayload, reset: () => void) => ReactNode;\n /** Transport invoked once per caught error. */\n transport?: CrashTransport;\n /** Additional context merged into every payload captured by this boundary. */\n context?: Record<string, unknown>;\n /** Called with both the raw error and the payload for custom side effects. */\n onError?: (error: unknown, payload: CrashPayload) => void;\n}\n\ninterface ErrorBoundaryState {\n payload: CrashPayload | null;\n}\n\n/**\n * React error boundary that captures render-phase errors into a structured\n * `CrashPayload`, forwards it to an optional transport, and renders the\n * caller-supplied fallback.\n */\nexport class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {\n state: ErrorBoundaryState = { payload: null };\n\n static getDerivedStateFromError(error: unknown): ErrorBoundaryState {\n return { payload: createCrashPayload(error) };\n }\n\n componentDidCatch(error: unknown, info: ErrorInfo): void {\n const context = {\n ...this.props.context,\n componentStack: info.componentStack ?? null,\n };\n const payload = createCrashPayload(error, context);\n this.setState({ payload });\n const { transport } = this.props;\n if (transport !== undefined) reportSafely(transport, payload, 'reporting');\n this.props.onError?.(error, payload);\n }\n\n reset = (): void => {\n this.setState({ payload: null });\n };\n\n render(): ReactNode {\n if (this.state.payload !== null) {\n return this.props.fallback(this.state.payload, this.reset);\n }\n return this.props.children;\n }\n}\n","import { createCrashPayload } from './crash-payload.js';\nimport { type CrashTransport, reportSafely } from './transport.js';\n\n/**\n * React Native's `global.ErrorUtils`, described structurally so this package\n * needs no React Native dependency or types.\n */\nexport interface ErrorUtilsLike {\n getGlobalHandler?(): ((error: unknown, isFatal?: boolean) => void) | undefined;\n setGlobalHandler(handler: (error: unknown, isFatal?: boolean) => void): void;\n}\n\n/**\n * What `installGlobalErrorCapture` needs from a global object.\n *\n * Both members are optional and independent, so one call handles either\n * platform: on web `globalThis` supplies `addEventListener`, on React Native it\n * supplies `ErrorUtils`, and in a test you pass a hand-built fake. Nothing here\n * references `window`, `document` or any RN type.\n */\nexport interface GlobalErrorTarget {\n addEventListener?(type: string, listener: (event: unknown) => void): void;\n removeEventListener?(type: string, listener: (event: unknown) => void): void;\n ErrorUtils?: ErrorUtilsLike;\n}\n\n/** Options for `installGlobalErrorCapture`. */\nexport interface GlobalCaptureOptions {\n /**\n * Where captured errors go. May be omitted at install and attached later with\n * `setTransport` — errors raised in between are held in the buffer below\n * rather than dropped.\n */\n transport?: CrashTransport;\n /**\n * How many pre-transport errors to retain. Default 20. The oldest is dropped\n * once full: a boot loop should not grow memory without bound, and the first\n * errors are the ones worth keeping.\n */\n bufferSize?: number;\n /** Context merged into every payload this capture produces. */\n context?: Record<string, unknown>;\n}\n\n/** Handle returned by `installGlobalErrorCapture`. */\nexport interface GlobalCaptureHandle {\n /**\n * Detach every handler this call installed and restore whatever was there\n * before. Safe to call more than once.\n */\n uninstall(): void;\n /** Attach or replace the transport, draining any buffered errors into it. */\n setTransport(transport: CrashTransport): void;\n /** How many errors are currently held awaiting a transport. */\n readonly buffered: number;\n}\n\n/** Pull the underlying error out of a structurally-typed DOM-ish event. */\nfunction unwrapEvent(event: unknown, key: 'error' | 'reason'): unknown {\n if (typeof event === 'object' && event !== null && key in event) {\n const value = (event as Record<string, unknown>)[key];\n if (value !== undefined && value !== null) return value;\n }\n // An ErrorEvent with no `.error` still carries a message worth reporting.\n if (typeof event === 'object' && event !== null && 'message' in event) {\n return (event as Record<string, unknown>).message;\n }\n return event;\n}\n\n/**\n * Route uncaught errors and unhandled rejections into a `CrashTransport`.\n *\n * `ErrorBoundary` only sees errors thrown during React's render phase. The\n * majority of a game's crashes are not there — they are in the update loop, in\n * an asset load, in a promise nobody awaited — and before this nothing carried\n * them to a transport.\n *\n * Both platforms are handled through the same call:\n *\n * ```ts\n * const capture = installGlobalErrorCapture(globalThis, { transport });\n * // ...\n * capture.uninstall();\n * ```\n *\n * The React Native handler is **chained, not replaced**: whatever handler was\n * installed before still runs, because on RN that handler is what shows the\n * red box in dev and terminates the app on a fatal. Swallowing it would convert\n * a visible crash into a silent one.\n */\nexport function installGlobalErrorCapture(\n target: GlobalErrorTarget,\n options?: GlobalCaptureOptions,\n): GlobalCaptureHandle {\n const bufferSize = Math.max(0, options?.bufferSize ?? 20);\n const context = options?.context ?? {};\n let transport: CrashTransport | null = options?.transport ?? null;\n const pending: ReturnType<typeof createCrashPayload>[] = [];\n let installed = true;\n\n const emit = (error: unknown, extra?: Record<string, unknown>): void => {\n const payload = createCrashPayload(error, extra ? { ...context, ...extra } : { ...context });\n if (transport === null) {\n if (bufferSize === 0) return;\n if (pending.length === bufferSize) pending.shift();\n pending.push(payload);\n return;\n }\n reportSafely(transport, payload, 'reporting');\n };\n\n const onError = (event: unknown): void => {\n emit(unwrapEvent(event, 'error'), { source: 'error' });\n };\n const onRejection = (event: unknown): void => {\n emit(unwrapEvent(event, 'reason'), { source: 'unhandledrejection' });\n };\n\n const canListen =\n typeof target.addEventListener === 'function' &&\n typeof target.removeEventListener === 'function';\n if (canListen) {\n target.addEventListener?.('error', onError);\n target.addEventListener?.('unhandledrejection', onRejection);\n }\n\n const errorUtils = target.ErrorUtils;\n const previousHandler = errorUtils?.getGlobalHandler?.();\n if (errorUtils !== undefined) {\n errorUtils.setGlobalHandler((error: unknown, isFatal?: boolean) => {\n // Something may have re-captured a reference to this closure; after\n // uninstall it must only forward, never report.\n if (!installed) {\n previousHandler?.(error, isFatal);\n return;\n }\n emit(error, { source: 'errorUtils', isFatal: isFatal ?? false });\n // Chain, so the platform's own reporting still happens.\n previousHandler?.(error, isFatal);\n });\n }\n\n return {\n uninstall(): void {\n if (!installed) return;\n installed = false;\n if (canListen) {\n target.removeEventListener?.('error', onError);\n target.removeEventListener?.('unhandledrejection', onRejection);\n }\n if (errorUtils !== undefined) {\n // With nothing to restore, leave a no-op rather than the dead closure.\n errorUtils.setGlobalHandler(previousHandler ?? (() => {}));\n }\n },\n setTransport(next: CrashTransport): void {\n transport = next;\n while (pending.length > 0) {\n reportSafely(next, pending.shift()!, 'draining');\n }\n },\n get buffered(): number {\n return pending.length;\n },\n };\n}\n\n/**\n * A Sentry-browser-shaped client, described structurally so this package takes\n * no dependency on `@sentry/browser` (or on any particular client).\n */\nexport interface ErrorClientLike {\n captureException(error: unknown, hint?: unknown): unknown;\n}\n\n/**\n * Transport over an injected browser error client.\n *\n * `createSentryTransport` is native-only — it dynamically imports\n * `@sentry/react-native`, which does not resolve on web. This is the web half:\n * the caller supplies an already-initialised client, so this package neither\n * imports nor initialises one.\n *\n * ```ts\n * import * as Sentry from '@sentry/browser';\n * Sentry.init({ dsn });\n * const transport = createClientTransport(Sentry);\n * ```\n *\n * The payload is rebuilt into an `Error` because that is what such clients\n * group by; the original stack is preserved rather than replaced with this\n * function's own.\n */\nexport function createClientTransport(client: ErrorClientLike): CrashTransport {\n return {\n report(payload): void {\n const error = new Error(payload.message);\n error.name = payload.name;\n if (payload.stack !== null) error.stack = payload.stack;\n try {\n client.captureException(error, { extra: payload.context });\n } catch (err) {\n console.error('[@flare-engine/errors] client.captureException threw', err);\n }\n },\n };\n}\n","import type { CrashPayload } from '../crash-payload.js';\nimport type { CrashTransport } from '../transport.js';\nimport { createNoopTransport } from '../transport.js';\n\n/**\n * The shape of a Sentry integration this package relies on. Structural, so no\n * type from the optional `@sentry/react-native` peer leaks into the public API;\n * the SDK's own `Integration` objects satisfy it.\n */\nexport interface SentryIntegrationLike {\n name: string;\n}\n\nexport interface SentryTransportOptions {\n dsn?: string;\n environment?: string;\n release?: string;\n sampleRate?: number;\n enabled?: boolean;\n /**\n * Chooses the integrations `init` receives. `kept` is the ADR-0008 allowlist\n * (event processors only) applied to `defaults`, the SDK's own default list;\n * without this hook `init` receives `kept`. Returning `defaults`, or any\n * capture integration such as `ReactNativeErrorHandlers`, re-enables Sentry's\n * own global handlers, which report outside the transport and so outside any\n * consent check placed in front of it. A hook that throws is logged and\n * `kept` is used.\n */\n integrations?: (\n kept: SentryIntegrationLike[],\n defaults: SentryIntegrationLike[],\n ) => SentryIntegrationLike[];\n}\n\ntype SentryMod = typeof import('@sentry/react-native');\ntype IntegrationsFn = (defaults: SentryIntegrationLike[]) => SentryIntegrationLike[];\n\n// Sentry must send only what `report()` hands it (ADR-0008). The SDK's default\n// integrations include its own global error/rejection handlers, which report\n// outside the transport and so outside any consent check placed in front of\n// it, plus collectors (breadcrumbs, device context) that attach data to every\n// event. Only event processors that enrich an event already in flight are kept.\n// Fail-safe: a name that does not match on some SDK major only loses that\n// enrichment; an unknown name can never add a capture path.\nconst KEPT_INTEGRATIONS: ReadonlySet<string> = new Set([\n 'RewriteFrames',\n 'Release',\n 'DebugMeta',\n 'SdkInfo',\n 'Dedupe',\n 'InboundFilters',\n 'NativeLinkedErrors',\n 'EventOrigin',\n]);\n\nfunction keepEventProcessors(defaults: SentryIntegrationLike[]): SentryIntegrationLike[] {\n return defaults.filter((integration) => KEPT_INTEGRATIONS.has(integration.name));\n}\n\n// The SDK calls this inside `init`, so a throwing hook must be caught here or\n// it would read as an import failure and degrade the transport to noop.\nfunction withIntegrationsHook(\n hook: NonNullable<SentryTransportOptions['integrations']>,\n): IntegrationsFn {\n return (defaults) => {\n const kept = keepEventProcessors(defaults);\n try {\n return hook(kept, defaults);\n } catch (err) {\n console.error(\n '[@flare-engine/errors] SentryTransportOptions.integrations threw — using the default allowlist',\n err,\n );\n return kept;\n }\n };\n}\n\nasync function closeSdk(mod: SentryMod): Promise<void> {\n try {\n await mod.close();\n } catch (err) {\n console.error('[@flare-engine/errors] Sentry close failed', err);\n }\n}\n\nexport function createSentryTransport(opts: SentryTransportOptions): CrashTransport {\n if (!opts.dsn || opts.enabled === false) {\n return createNoopTransport();\n }\n\n const dsn = opts.dsn;\n const integrations =\n opts.integrations === undefined ? keepEventProcessors : withIntegrationsHook(opts.integrations);\n let mod: SentryMod | null = null;\n let loadFailed = false;\n let closed = false;\n let closing: Promise<void> | null = null;\n\n return {\n async report(payload: CrashPayload): Promise<void> {\n if (loadFailed || closed) return;\n\n if (mod === null) {\n try {\n const loaded = await import('@sentry/react-native');\n // `close()` may have run while the import was pending; starting a\n // client now would leave one nothing can shut down.\n if (closed) return;\n loaded.init({\n dsn,\n environment: opts.environment,\n release: opts.release,\n sampleRate: opts.sampleRate,\n integrations,\n enableAutoSessionTracking: false,\n enableNativeCrashHandling: false,\n enableAppHangTracking: false,\n enableWatchdogTerminationTracking: false,\n sendClientReports: false,\n });\n mod = loaded;\n } catch {\n console.error(\n '[@flare-engine/errors] Failed to load @sentry/react-native — degrading to noop',\n );\n loadFailed = true;\n return;\n }\n }\n\n try {\n const err = Object.assign(new Error(payload.message), { name: payload.name });\n if (payload.stack !== null) err.stack = payload.stack;\n mod.captureException(err, {\n extra: { ...payload.context, capturedAt: payload.timestamp },\n });\n } catch {\n console.error('[@flare-engine/errors] Sentry captureException failed');\n }\n },\n close(): Promise<void> {\n if (closing === null) {\n closed = true;\n closing = mod === null ? Promise.resolve() : closeSdk(mod);\n }\n return closing;\n },\n };\n}\n"],"mappings":";;AAEA,SAAgB,kBAAkB,GAAG,YAA8C;CACjF,OAAO;EACL,MAAM,OAAO,SA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A2B;KAC3B,2BAA2B;KAC3B,uBAAuB;KACvB,mCAAmC;KACnC,mBAAmB;IACrB,CAAC;IACD,MAAM;GACR,QAAQ;IACN,QAAQ,MACN,gFACF;IACA,aAAa;IACb;GACF;GAGF,IAAI;IACF,MAAM,MAAM,OAAO,OAAO,IAAI,MAAM,QAAQ,OAAO,GAAG,EAAE,MAAM,QAAQ,KAAK,CAAC;IAC5E,IAAI,QAAQ,UAAU,MAAM,IAAI,QAAQ,QAAQ;IAChD,IAAI,iBAAiB,KAAK,EACxB,OAAO;KAAE,GAAG,QAAQ;KAAS,YAAY,QAAQ;IAAU,EAC7D,CAAC;GACH,QAAQ;IACN,QAAQ,MAAM,uDAAuD;GACvE;EACF;EACA,QAAuB;GACrB,IAAI,YAAY,MAAM;IACpB,SAAS;IACT,UAAU,QAAQ,OAAO,QAAQ,QAAQ,IAAI,SAAS,GAAG;GAC3D;GACA,OAAO;EACT;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/errors",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10",
|
|
4
4
|
"description": "React error boundary, structured crash payload, pluggable crash transport",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"flare-engine",
|
|
7
|
+
"errors",
|
|
8
|
+
"error-boundary",
|
|
9
|
+
"crash-report",
|
|
10
|
+
"sentry",
|
|
11
|
+
"react-native",
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
6
14
|
"license": "Apache-2.0",
|
|
7
15
|
"author": "Grzegorz Otto",
|
|
8
16
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|