@error-reporting/node 0.0.0-stage → 0.2.0

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 CHANGED
@@ -1,3 +1,17 @@
1
- # Temporary Holding Version
1
+ # @error-reporting/node
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ```sh
4
+ npm install @error-reporting/node
5
+ ```
6
+
7
+ ```js
8
+ import { Init, CaptureException, Flush } from '@error-reporting/node'
9
+
10
+ Init({ dsn: process.env.ERROR_REPORTING_DSN, environment: 'production', release: 'git-sha' })
11
+ CaptureException(new Error('上报接入测试'), { requestId: 'request-42' })
12
+ await Flush()
13
+ ```
14
+
15
+ 在业务模块加载前调用一次 `Init`。SDK 自动捕获进程内未处理异常,并通过 `CaptureException` 补报已处理的异常。短脚本或进程退出前可用 `Flush(timeoutMs)` 等待发送。
16
+
17
+ 从 0.1.x 升级到 0.2.x 时,把三个入口替换成示例中的大写开头写法。
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { CaptureContext, Options } from '@error-reporting/core'
2
+
3
+ export type { CaptureContext, Options, ReportEvent } from '@error-reporting/core'
4
+ export function Init(options?: Options): void
5
+ export function CaptureException(error: unknown, context?: CaptureContext): string
6
+ export function Flush(timeout?: number): PromiseLike<boolean>
package/package.json CHANGED
@@ -1,6 +1,12 @@
1
1
  {
2
2
  "name": "@error-reporting/node",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Error Reporting Platform SDK for Node.js",
5
+ "type": "module",
6
+ "engines": { "node": ">=22.12.0 <23 || >=23.2.0" },
7
+ "exports": { ".": { "types": "./index.d.ts", "default": "./src/index.js" } },
8
+ "files": ["src", "index.d.ts"],
9
+ "dependencies": { "@error-reporting/core": "0.2.0", "@sentry/node": "^11.17.0" },
10
+ "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" },
11
+ "repository": { "type": "git", "url": "git+https://gitee.com/qglove/error-reporting-platform.git", "directory": "packages/javascript/node" }
12
+ }
package/src/index.js ADDED
@@ -0,0 +1,40 @@
1
+ import * as Sentry from '@sentry/node'
2
+ import { CreateReportOptions } from '@error-reporting/core'
3
+
4
+ let initialized = false
5
+
6
+ /** 在 Node.js 业务入口加载前安装默认异常监听。
7
+ * @param {import('@error-reporting/core').Options} options 显式配置优先于服务器环境变量。
8
+ */
9
+ export function Init(options = {}) {
10
+ const config = CreateReportOptions({
11
+ dsn: process.env.ERROR_REPORTING_DSN,
12
+ environment: process.env.NODE_ENV ?? 'development',
13
+ release: process.env.ERROR_REPORTING_RELEASE,
14
+ service: process.env.ERROR_REPORTING_SERVICE,
15
+ ...options,
16
+ }, 'node')
17
+
18
+ if (initialized) {
19
+ throw new Error('Node.js error reporting is already initialized')
20
+ }
21
+
22
+ Sentry.init(config)
23
+
24
+ initialized = true
25
+ }
26
+
27
+ /** 上报已被业务接住的错误。
28
+ * @param {unknown} error 原始异常或拒绝原因。
29
+ * @param {import('@error-reporting/core').CaptureContext} context 诊断用请求编号。
30
+ */
31
+ export function CaptureException(error, context = {}) {
32
+ return Sentry.captureException(error, context.requestId ? { tags: { request_id: context.requestId } } : undefined)
33
+ }
34
+
35
+ /** 等待待发送队列,适用于进程优雅退出和短脚本。
36
+ * @param {number} timeout 等待毫秒数。
37
+ */
38
+ export function Flush(timeout = 2000) {
39
+ return Sentry.flush(timeout)
40
+ }