@flare-engine/errors

中文文档
0.4.12发布于 2026/11/13Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2470

React error boundary, structured crash payload, pluggable crash transport

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Catching what the boundary cannot
  4. License

@flare-engine/errors

React error boundary + structured crash payloads. createCrashPayload(err, context?) normalizes any thrown value into a stable CrashPayload shape. Pluggable CrashTransport sinks: createNoopTransport, createConsoleTransport, createSentryTransport (lazy-loaded @sentry/react-native, optional peer dep), and composeTransports for fan-out.

Install

bun add @flare-engine/errors

Optional peer deps: react, @sentry/react-native (only if you wire the Sentry transport).

Usage

import { ErrorBoundary, createSentryTransport, createConsoleTransport,
         composeTransports, type CrashPayload } from '@flare-engine/errors';
import type { ReactNode } from 'react';

declare function CrashScreen(props: { err: CrashPayload; onRetry: () => void }): ReactNode;
declare function App(): ReactNode;

const transport = composeTransports(
  createConsoleTransport(),
  createSentryTransport({ dsn: 'https://[email protected]/0', enabled: true }),
);

<ErrorBoundary
  fallback={(err, reset) => <CrashScreen err={err} onRetry={reset} />}
  transport={transport}
  context={{ scene: 'play', level: 4 }}
>
  <App />
</ErrorBoundary>

createSentryTransport starts Sentry with the SDK's own capture switched off — no global error or rejection handlers, no automatic session tracking, no native crash handling, no console/network instrumentation — so Sentry sends only what the transport is given, and a consent check placed in front of the transport holds. Process-wide JS errors reach it through installGlobalErrorCapture (next section). The one exception the transport cannot close: on Android 11+ the native SDK reports past ANRs on its own; if consent must cover those, set the io.sentry.anr.enable meta-data to false in your Android manifest (Sentry Android docs).

Opting a dropped integration back in is a deliberate act: the integrations hook receives the allowlist (kept) and the SDK's own default list (defaults) and returns what init uses. Each entry is a SentryIntegrationLike ({ name }), so no @sentry/react-native type is needed.

import { createSentryTransport } from '@flare-engine/errors';

const transport = createSentryTransport({
  dsn: 'https://[email protected]/0',
  integrations: (kept, defaults) => [
    ...kept,
    ...defaults.filter((integration) => integration.name === 'Breadcrumbs'),
  ],
});

Returning defaults, or passing through any capture integration such as ReactNativeErrorHandlers, re-enables Sentry's own global handlers, which report outside the transport and so outside any consent check placed in front of it. A hook that throws is logged and the allowlist is used.

On consent revocation, close the transport. The Sentry transport awaits the SDK's close() once (only if it was ever loaded), and every later report() sends nothing; a second close() resolves without closing again. composeTransports forwards close() to each child that has one, so the chain from the first example shuts down in one call:

import { composeTransports, createConsoleTransport, createSentryTransport } from '@flare-engine/errors';

const transport = composeTransports(
  createConsoleTransport(),
  createSentryTransport({ dsn: 'https://[email protected]/0' }),
);

// later, when the user withdraws consent:
await transport.close?.();

close is optional on CrashTransport; createNoopTransport, createConsoleTransport and the noop a DSN-less createSentryTransport returns leave it undefined.

Catching what the boundary cannot

ErrorBoundary only sees errors thrown during React's render phase. Most of a game's crashes are not there — they are in the update loop, in an asset load, in a promise nobody awaited. installGlobalErrorCapture routes those into the same transport chain.

import { installGlobalErrorCapture, createConsoleTransport } from '@flare-engine/errors';

// `globalThis` supplies `addEventListener` on web and `ErrorUtils` on React
// Native, so one call covers both.
const capture = installGlobalErrorCapture(globalThis, {
  transport: createConsoleTransport(),
  context: { build: '1.2.3' },
});

// Later, e.g. on scene teardown or hot reload:
capture.uninstall();

The transport can attach after installing. Errors raised in between are held in a small ring buffer (default 20, oldest dropped) and drained on attach, so the crashes that happen during boot — usually the interesting ones — are not lost while your reporter is still initialising:

import { installGlobalErrorCapture, createClientTransport } from '@flare-engine/errors';

declare const Sentry: { captureException(error: unknown, hint?: unknown): unknown };

const capture = installGlobalErrorCapture(globalThis);   // no transport yet
// ... boot, read config, initialise the reporter ...
capture.setTransport(createClientTransport(Sentry));     // buffered errors drain here

Two things worth knowing:

  • On React Native the previous ErrorUtils handler is chained, not replaced — that handler is what shows the red box in development and ends the process on a fatal, so it still runs. uninstall() restores it.
  • createSentryTransport is native-only (it imports @sentry/react-native). createClientTransport is the web counterpart: you initialise a browser client and inject it, so this package depends on no reporter of its own.

See API spec.

License

Apache-2.0

版本与发布记录

@flare-engine/errors 共发布 17 个版本,首次发布于 2026/06/10;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.12latest2026/11/133 天前
0.4.112026/11/123 天前
0.4.102026/11/087 天前
0.4.82026/11/0610 天前
0.4.72026/11/0411 天前
0.4.62026/10/3115 天前

关于 @flare-engine/errors 的常见问题

@flare-engine/errors 是什么?

React error boundary, structured crash payload, pluggable crash transport

如何安装 @flare-engine/errors?

在项目目录运行 npm install @flare-engine/errors,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flare-engine/errors 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flare-engine/errors 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flare-engine/errors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flare-engine/errors 可以商用吗?

@flare-engine/errors 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@flare-engine/errors 的下载量有多少?

根据 npm 官方下载统计,@flare-engine/errors 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,470 次,日均约 82 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flare-engine/errors 还在维护吗?

最新版本 0.4.12 发布于 2026/11/13(3 天前);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。