@molecule/api-error-tracking 1.0.0 → 1.0.2
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 +333 -0
- package/package.json +9 -8
package/README.md
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:48:08.891Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-error-tracking
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Error tracking core interface for molecule.dev.
|
|
16
|
+
|
|
17
|
+
Defines the standard interface for error tracking / crash reporting
|
|
18
|
+
providers (Sentry, console, etc.) plus never-throwing convenience
|
|
19
|
+
functions (`captureException`, `captureMessage`, `setUser`, `flush`)
|
|
20
|
+
that delegate to the bonded provider.
|
|
21
|
+
|
|
22
|
+
This is distinct from `@molecule/api-monitoring`, which is health checks
|
|
23
|
+
(is the database up?). Error tracking captures individual unexpected
|
|
24
|
+
exceptions with context so they can be aggregated and triaged.
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { setProvider, captureException, captureMessage } from '@molecule/api-error-tracking'
|
|
30
|
+
import { provider } from '@molecule/api-error-tracking-sentry'
|
|
31
|
+
|
|
32
|
+
// Bond a provider at startup (skip this and every capture is a no-op)
|
|
33
|
+
setProvider(provider)
|
|
34
|
+
|
|
35
|
+
// Report an unexpected exception with normalized context
|
|
36
|
+
try {
|
|
37
|
+
await chargeCustomer(order)
|
|
38
|
+
} catch (error) {
|
|
39
|
+
captureException(error, {
|
|
40
|
+
tags: { source: 'billing' },
|
|
41
|
+
user: { id: order.userId },
|
|
42
|
+
extra: { orderId: order.id },
|
|
43
|
+
})
|
|
44
|
+
throw error
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Report a standalone message
|
|
48
|
+
captureMessage('Payment retry queue is backing up', 'warning')
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Type
|
|
52
|
+
|
|
53
|
+
`core`
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npm install @molecule/api-error-tracking @molecule/api-bond @molecule/api-i18n
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
### Interfaces
|
|
64
|
+
|
|
65
|
+
#### `ErrorTrackingContext`
|
|
66
|
+
|
|
67
|
+
Normalized context attached to a captured exception or message.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface ErrorTrackingContext {
|
|
71
|
+
/** Short, indexable key/value pairs (e.g. `{ source: 'express' }`). */
|
|
72
|
+
tags?: Record<string, string | number | boolean>
|
|
73
|
+
|
|
74
|
+
/** The user the event occurred for. */
|
|
75
|
+
user?: ErrorTrackingUser
|
|
76
|
+
|
|
77
|
+
/** Arbitrary additional (non-indexed) structured data. */
|
|
78
|
+
extra?: Record<string, unknown>
|
|
79
|
+
|
|
80
|
+
/** The request the event occurred in. */
|
|
81
|
+
request?: ErrorTrackingRequestContext
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### `ErrorTrackingProvider`
|
|
86
|
+
|
|
87
|
+
Error tracking provider interface.
|
|
88
|
+
|
|
89
|
+
All error tracking providers must implement this interface. Providers
|
|
90
|
+
receive the normalized {@link ErrorTrackingContext} and map it to their
|
|
91
|
+
own event model (tags/user/extra scopes, etc.).
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
interface ErrorTrackingProvider {
|
|
95
|
+
/**
|
|
96
|
+
* Reports an exception (or any thrown value) to the tracking backend.
|
|
97
|
+
*
|
|
98
|
+
* @param error - The thrown value to report.
|
|
99
|
+
* @param context - Optional normalized context (tags/user/extra/request).
|
|
100
|
+
* @returns The backend's event id when available, otherwise `undefined`.
|
|
101
|
+
*/
|
|
102
|
+
captureException(error: unknown, context?: ErrorTrackingContext): string | void
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Reports a standalone message (no exception object) to the tracking backend.
|
|
106
|
+
*
|
|
107
|
+
* @param message - The message to report.
|
|
108
|
+
* @param level - Severity level (providers default to `'info'` when omitted).
|
|
109
|
+
* @param context - Optional normalized context (tags/user/extra/request).
|
|
110
|
+
* @returns The backend's event id when available, otherwise `undefined`.
|
|
111
|
+
*/
|
|
112
|
+
captureMessage(
|
|
113
|
+
message: string,
|
|
114
|
+
level?: ErrorTrackingLevel,
|
|
115
|
+
context?: ErrorTrackingContext,
|
|
116
|
+
): string | void
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Optionally associates subsequent captures with a user (`null` clears it).
|
|
120
|
+
* Providers whose backend has no user scoping may leave this undefined.
|
|
121
|
+
*
|
|
122
|
+
* @param user - The user to associate, or `null` to clear.
|
|
123
|
+
*/
|
|
124
|
+
setUser?(user: ErrorTrackingUser | null): void
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Optionally flushes buffered events to the backend — call before process
|
|
128
|
+
* exit so queued reports aren't lost. Providers that deliver synchronously
|
|
129
|
+
* may leave this undefined.
|
|
130
|
+
*
|
|
131
|
+
* @param timeoutMs - Maximum time to wait for delivery.
|
|
132
|
+
* @returns `true` when everything flushed within the timeout.
|
|
133
|
+
*/
|
|
134
|
+
flush?(timeoutMs?: number): Promise<boolean>
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### `ErrorTrackingRequestContext`
|
|
139
|
+
|
|
140
|
+
Normalized, provider-agnostic description of the HTTP request (or
|
|
141
|
+
request-like operation) an event occurred in.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
interface ErrorTrackingRequestContext {
|
|
145
|
+
/** HTTP method (e.g. `GET`). */
|
|
146
|
+
method?: string
|
|
147
|
+
|
|
148
|
+
/** Request URL or path, including the query string (e.g. `/api/users/123?full=true`). */
|
|
149
|
+
url?: string
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Selected request headers. Callers must NOT include credential-bearing
|
|
153
|
+
* headers (`cookie`, `authorization`) — error trackers are third-party
|
|
154
|
+
* sinks and must never receive session material.
|
|
155
|
+
*/
|
|
156
|
+
headers?: Record<string, string | string[] | undefined>
|
|
157
|
+
|
|
158
|
+
/** Parsed query parameters. */
|
|
159
|
+
query?: Record<string, unknown>
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### `ErrorTrackingUser`
|
|
164
|
+
|
|
165
|
+
Normalized description of the user an event occurred for.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
interface ErrorTrackingUser {
|
|
169
|
+
/** Application-level user id. */
|
|
170
|
+
id?: string
|
|
171
|
+
|
|
172
|
+
/** The user's email address. */
|
|
173
|
+
email?: string
|
|
174
|
+
|
|
175
|
+
/** The user's username / display handle. */
|
|
176
|
+
username?: string
|
|
177
|
+
|
|
178
|
+
/** The user's IP address. */
|
|
179
|
+
ipAddress?: string
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Types
|
|
184
|
+
|
|
185
|
+
#### `ErrorTrackingLevel`
|
|
186
|
+
|
|
187
|
+
Severity level for a captured message or exception.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
type ErrorTrackingLevel = 'fatal' | 'error' | 'warning' | 'info' | 'debug'
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Functions
|
|
194
|
+
|
|
195
|
+
#### `captureException(error, context)`
|
|
196
|
+
|
|
197
|
+
Reports an exception (or any thrown value) to the bonded error tracking
|
|
198
|
+
provider. Silent no-op when no provider is bonded; never throws.
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
function captureException(error: unknown, context?: ErrorTrackingContext): string | undefined
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- `error` — The thrown value to report.
|
|
205
|
+
- `context` — Optional normalized context (tags/user/extra/request).
|
|
206
|
+
|
|
207
|
+
**Returns:** The backend's event id when available, otherwise `undefined`.
|
|
208
|
+
|
|
209
|
+
#### `captureMessage(message, level, context)`
|
|
210
|
+
|
|
211
|
+
Reports a standalone message to the bonded error tracking provider.
|
|
212
|
+
Silent no-op when no provider is bonded; never throws.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
function captureMessage(
|
|
216
|
+
message: string,
|
|
217
|
+
level?: ErrorTrackingLevel,
|
|
218
|
+
context?: ErrorTrackingContext,
|
|
219
|
+
): string | undefined
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- `message` — The message to report.
|
|
223
|
+
- `level` — Severity level (providers default to `'info'` when omitted).
|
|
224
|
+
- `context` — Optional normalized context (tags/user/extra/request).
|
|
225
|
+
|
|
226
|
+
**Returns:** The backend's event id when available, otherwise `undefined`.
|
|
227
|
+
|
|
228
|
+
#### `flush(timeoutMs)`
|
|
229
|
+
|
|
230
|
+
Flushes buffered events to the backend — call before process exit so
|
|
231
|
+
queued reports aren't lost. Resolves `true` when no provider is bonded or
|
|
232
|
+
the provider delivers synchronously (nothing to flush); never rejects.
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
function flush(timeoutMs?: number): Promise<boolean>
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
- `timeoutMs` — Maximum time to wait for delivery.
|
|
239
|
+
|
|
240
|
+
**Returns:** `true` when everything flushed within the timeout.
|
|
241
|
+
|
|
242
|
+
#### `getOptionalProvider()`
|
|
243
|
+
|
|
244
|
+
Retrieves the bonded error tracking provider, returning `null` if none is
|
|
245
|
+
bonded. Prefer this over `getProvider()` in optional reporting code paths.
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
function getOptionalProvider(): ErrorTrackingProvider | null
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
**Returns:** The bonded error tracking provider, or `null`.
|
|
252
|
+
|
|
253
|
+
#### `getProvider()`
|
|
254
|
+
|
|
255
|
+
Retrieves the bonded error tracking provider, throwing if none is
|
|
256
|
+
configured. Application code should normally use the never-throwing
|
|
257
|
+
convenience functions (`captureException`/`captureMessage`) or
|
|
258
|
+
`getOptionalProvider()` instead.
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
function getProvider(): ErrorTrackingProvider
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
**Returns:** The bonded error tracking provider.
|
|
265
|
+
|
|
266
|
+
#### `hasProvider()`
|
|
267
|
+
|
|
268
|
+
Checks whether an error tracking provider is currently bonded.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
function hasProvider(): boolean
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**Returns:** `true` if an error tracking provider is bonded.
|
|
275
|
+
|
|
276
|
+
#### `setProvider(provider)`
|
|
277
|
+
|
|
278
|
+
Registers an error tracking provider as the active singleton. Called by
|
|
279
|
+
bond packages during application startup.
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
function setProvider(provider: ErrorTrackingProvider): void
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
- `provider` — The error tracking provider implementation to bond.
|
|
286
|
+
|
|
287
|
+
#### `setUser(user)`
|
|
288
|
+
|
|
289
|
+
Associates subsequent captures with a user (`null` clears it). Silent
|
|
290
|
+
no-op when no provider is bonded or the provider has no user scoping;
|
|
291
|
+
never throws.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
function setUser(user: ErrorTrackingUser | null): void
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
- `user` — The user to associate, or `null` to clear.
|
|
298
|
+
|
|
299
|
+
## Available Providers
|
|
300
|
+
|
|
301
|
+
| Provider | Package |
|
|
302
|
+
| -------- | -------------------------------------- |
|
|
303
|
+
| Console | `@molecule/api-error-tracking-console` |
|
|
304
|
+
| Sentry | `@molecule/api-error-tracking-sentry` |
|
|
305
|
+
|
|
306
|
+
## Injection Notes
|
|
307
|
+
|
|
308
|
+
### Requirements
|
|
309
|
+
|
|
310
|
+
Peer dependencies:
|
|
311
|
+
|
|
312
|
+
- `@molecule/api-bond` ^1.0.1
|
|
313
|
+
- `@molecule/api-i18n` ^1.0.1
|
|
314
|
+
|
|
315
|
+
### Runtime Dependencies
|
|
316
|
+
|
|
317
|
+
- `@molecule/api-bond`
|
|
318
|
+
- `@molecule/api-i18n`
|
|
319
|
+
|
|
320
|
+
- **The convenience functions NEVER throw and no-op when unbonded.** Error
|
|
321
|
+
tracking is a diagnostic side-channel: an app without a bonded tracker
|
|
322
|
+
(or with a broken one) must behave exactly as if the calls weren't
|
|
323
|
+
there. Do NOT wrap `captureException` in defensive try/catch — it is
|
|
324
|
+
already safe to call anywhere, including inside error middleware.
|
|
325
|
+
- The default Express error path (`@molecule/api-server-default-express`)
|
|
326
|
+
already calls `captureException` for genuine unexpected errors (untagged
|
|
327
|
+
500s, uncaught exceptions, unhandled rejections). Tagged config-missing
|
|
328
|
+
503s and 4xx responses are deliberately NOT captured — they are expected,
|
|
329
|
+
user-actionable conditions, not defects.
|
|
330
|
+
- `getProvider()` throws when unbonded (like other cores); prefer the
|
|
331
|
+
convenience functions or `getOptionalProvider()` in reporting paths.
|
|
332
|
+
- Context is normalized (`tags`/`user`/`extra`/`request`) — never pass
|
|
333
|
+
provider-specific (e.g. Sentry) scope objects through this interface.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-error-tracking",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Error tracking core interface for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"dist"
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
21
22
|
],
|
|
22
23
|
"keywords": [
|
|
23
24
|
"molecule",
|
|
@@ -28,22 +29,22 @@
|
|
|
28
29
|
],
|
|
29
30
|
"license": "Apache-2.0",
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@molecule/api-bond": "^1.0.
|
|
32
|
-
"@molecule/api-i18n": "^1.0.
|
|
32
|
+
"@molecule/api-bond": "^1.0.1",
|
|
33
|
+
"@molecule/api-i18n": "^1.0.1"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@molecule/api-bond": "1.0.
|
|
36
|
-
"@molecule/api-i18n": "1.0.
|
|
36
|
+
"@molecule/api-bond": "1.0.2",
|
|
37
|
+
"@molecule/api-i18n": "1.0.3",
|
|
37
38
|
"@types/node": "26.1.2",
|
|
38
39
|
"typescript": "6.0.3",
|
|
39
|
-
"vitest": "4.1.
|
|
40
|
+
"vitest": "4.1.11"
|
|
40
41
|
},
|
|
41
42
|
"repository": {
|
|
42
43
|
"type": "git",
|
|
43
44
|
"url": "https://github.com/molecule-dev/molecule.git",
|
|
44
45
|
"directory": "packages/api/core/error-tracking"
|
|
45
46
|
},
|
|
46
|
-
"homepage": "https://
|
|
47
|
+
"homepage": "https://www.molecule.dev/packages/api-error-tracking",
|
|
47
48
|
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
48
49
|
"publishConfig": {
|
|
49
50
|
"access": "public"
|