autotel 4.1.0 → 4.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.
Files changed (154) hide show
  1. package/package.json +1 -2
  2. package/src/attribute-redacting-processor.test.ts +0 -763
  3. package/src/attribute-redacting-processor.ts +0 -621
  4. package/src/attributes/attachers.ts +0 -161
  5. package/src/attributes/builders.ts +0 -529
  6. package/src/attributes/domains.ts +0 -42
  7. package/src/attributes/index.ts +0 -81
  8. package/src/attributes/registry.ts +0 -323
  9. package/src/attributes/types.ts +0 -211
  10. package/src/attributes/utils.ts +0 -64
  11. package/src/attributes/validators.ts +0 -266
  12. package/src/attributes.test.ts +0 -292
  13. package/src/auto.ts +0 -67
  14. package/src/autotel-logger.test.ts +0 -548
  15. package/src/autotel-logger.ts +0 -364
  16. package/src/baggage-span-processor.test.ts +0 -202
  17. package/src/baggage-span-processor.ts +0 -100
  18. package/src/business-baggage.test.ts +0 -500
  19. package/src/business-baggage.ts +0 -669
  20. package/src/circuit-breaker.test.ts +0 -341
  21. package/src/circuit-breaker.ts +0 -184
  22. package/src/config.test.ts +0 -94
  23. package/src/config.ts +0 -172
  24. package/src/correlated-events.test.ts +0 -151
  25. package/src/correlated-events.ts +0 -47
  26. package/src/correlation-id.test.ts +0 -163
  27. package/src/correlation-id.ts +0 -206
  28. package/src/db.test.ts +0 -252
  29. package/src/db.ts +0 -447
  30. package/src/decorators.test.ts +0 -153
  31. package/src/decorators.ts +0 -188
  32. package/src/define-event.test.ts +0 -41
  33. package/src/define-event.ts +0 -58
  34. package/src/devtools.ts +0 -60
  35. package/src/drain-pipeline.test.ts +0 -68
  36. package/src/drain-pipeline.ts +0 -199
  37. package/src/drain-toolkit.test.ts +0 -113
  38. package/src/drain-toolkit.ts +0 -129
  39. package/src/enricher-toolkit.test.ts +0 -67
  40. package/src/enricher-toolkit.ts +0 -79
  41. package/src/enrichers.test.ts +0 -150
  42. package/src/enrichers.ts +0 -145
  43. package/src/env-config.test.ts +0 -323
  44. package/src/env-config.ts +0 -309
  45. package/src/error-catalog.test.ts +0 -133
  46. package/src/error-catalog.ts +0 -262
  47. package/src/event-queue.test.ts +0 -864
  48. package/src/event-queue.ts +0 -699
  49. package/src/event-subscriber.ts +0 -262
  50. package/src/event-testing.ts +0 -197
  51. package/src/event.test.ts +0 -1104
  52. package/src/event.ts +0 -988
  53. package/src/events-config.ts +0 -235
  54. package/src/exporters.ts +0 -165
  55. package/src/filtering-span-processor.test.ts +0 -281
  56. package/src/filtering-span-processor.ts +0 -111
  57. package/src/flatten-attributes.test.ts +0 -76
  58. package/src/flatten-attributes.ts +0 -80
  59. package/src/functional.strict-types.typecheck.ts +0 -53
  60. package/src/functional.test.ts +0 -1464
  61. package/src/functional.ts +0 -2539
  62. package/src/functional.types.test.ts +0 -135
  63. package/src/hook.mjs +0 -15
  64. package/src/http.test.ts +0 -485
  65. package/src/http.ts +0 -424
  66. package/src/index.ts +0 -433
  67. package/src/init-auto-redactor.test.ts +0 -53
  68. package/src/init-redactor.test.ts +0 -8
  69. package/src/init.customization.test.ts +0 -665
  70. package/src/init.integrations.test.ts +0 -399
  71. package/src/init.openllmetry.test.ts +0 -194
  72. package/src/init.protocol.test.ts +0 -215
  73. package/src/init.ts +0 -2439
  74. package/src/instrumentation.test.ts +0 -108
  75. package/src/instrumentation.ts +0 -319
  76. package/src/logger.test.ts +0 -125
  77. package/src/logger.ts +0 -341
  78. package/src/messaging-adapters.test.ts +0 -595
  79. package/src/messaging-adapters.ts +0 -583
  80. package/src/messaging-testing.test.ts +0 -573
  81. package/src/messaging-testing.ts +0 -935
  82. package/src/messaging.test.ts +0 -1646
  83. package/src/messaging.ts +0 -2245
  84. package/src/metric-helpers.ts +0 -47
  85. package/src/metric-testing.ts +0 -197
  86. package/src/metric.ts +0 -446
  87. package/src/metrics.test.ts +0 -241
  88. package/src/node-require.ts +0 -123
  89. package/src/operation-context.ts +0 -93
  90. package/src/parse-error.test.ts +0 -73
  91. package/src/parse-error.ts +0 -112
  92. package/src/posthog-logs.test.ts +0 -115
  93. package/src/posthog-logs.ts +0 -77
  94. package/src/pretty-console-exporter.test.ts +0 -545
  95. package/src/pretty-console-exporter.ts +0 -413
  96. package/src/pretty-log-formatter.test.ts +0 -123
  97. package/src/pretty-log-formatter.ts +0 -210
  98. package/src/processors/canonical-log-line-processor.test.ts +0 -523
  99. package/src/processors/canonical-log-line-processor.ts +0 -396
  100. package/src/processors.ts +0 -152
  101. package/src/rate-limiter.test.ts +0 -199
  102. package/src/rate-limiter.ts +0 -98
  103. package/src/redact-values.test.ts +0 -90
  104. package/src/redact-values.ts +0 -34
  105. package/src/register.ts +0 -37
  106. package/src/request-logger.test.ts +0 -545
  107. package/src/request-logger.ts +0 -342
  108. package/src/sampling.test.ts +0 -1060
  109. package/src/sampling.ts +0 -737
  110. package/src/security-schema.test.ts +0 -45
  111. package/src/security-schema.ts +0 -107
  112. package/src/semantic-conventions.ts +0 -15
  113. package/src/semantic-helpers.test.ts +0 -226
  114. package/src/semantic-helpers.ts +0 -438
  115. package/src/shutdown.test.ts +0 -364
  116. package/src/shutdown.ts +0 -246
  117. package/src/span-name-normalizer.test.ts +0 -377
  118. package/src/span-name-normalizer.ts +0 -213
  119. package/src/stable-hash.ts +0 -27
  120. package/src/structured-error.test.ts +0 -191
  121. package/src/structured-error.ts +0 -157
  122. package/src/stub.integration.test.ts +0 -361
  123. package/src/tail-sampling-processor.test.ts +0 -230
  124. package/src/tail-sampling-processor.ts +0 -55
  125. package/src/test-span-collector.test.ts +0 -234
  126. package/src/test-span-collector.ts +0 -150
  127. package/src/testing.ts +0 -705
  128. package/src/trace-context.test.ts +0 -73
  129. package/src/trace-context.ts +0 -567
  130. package/src/trace-helpers.new.test.ts +0 -278
  131. package/src/trace-helpers.test.ts +0 -290
  132. package/src/trace-helpers.ts +0 -710
  133. package/src/trace-hybrid.test.ts +0 -42
  134. package/src/trace-hybrid.ts +0 -37
  135. package/src/tracer-provider.test.ts +0 -183
  136. package/src/tracer-provider.ts +0 -266
  137. package/src/track.test.ts +0 -154
  138. package/src/track.ts +0 -216
  139. package/src/validate.test.ts +0 -287
  140. package/src/validate.ts +0 -307
  141. package/src/validation-attributes.ts +0 -43
  142. package/src/validation.test.ts +0 -330
  143. package/src/validation.ts +0 -246
  144. package/src/variable-name-inference.test.ts +0 -178
  145. package/src/variable-name-inference.ts +0 -242
  146. package/src/webhook.test.ts +0 -649
  147. package/src/webhook.ts +0 -637
  148. package/src/workflow-distributed.test.ts +0 -786
  149. package/src/workflow-distributed.ts +0 -916
  150. package/src/workflow.async-safety.integration.test.ts +0 -345
  151. package/src/workflow.test.ts +0 -647
  152. package/src/workflow.ts +0 -810
  153. package/src/yaml-config.test.ts +0 -373
  154. package/src/yaml-config.ts +0 -351
@@ -1,241 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import { Metric, getMetrics, resetMetrics } from './metric';
3
- import { type ILogger } from './logger';
4
- import { init } from './init';
5
- import { configure } from './config';
6
-
7
- describe('Metrics', () => {
8
- let mockLogger: ILogger;
9
-
10
- beforeEach(() => {
11
- resetMetrics();
12
- init({ service: 'test-app' });
13
- configure({
14
- meterName: 'test',
15
- });
16
- mockLogger = {
17
- info: vi.fn(),
18
- warn: vi.fn(),
19
- error: vi.fn(),
20
- debug: vi.fn(),
21
- };
22
- });
23
-
24
- describe('trackEvent', () => {
25
- it('should track business events as metrics', () => {
26
- const metrics = new Metric('test-service', { logger: mockLogger });
27
-
28
- metrics.trackEvent('order.completed', {
29
- orderId: '123',
30
- amount: 99.99,
31
- });
32
-
33
- // Pino-native: (extra, message)
34
- expect(mockLogger.info).toHaveBeenCalledWith(
35
- {
36
- event: 'order.completed',
37
- attributes: { orderId: '123', amount: 99.99 },
38
- },
39
- 'Metric event tracked',
40
- );
41
- });
42
-
43
- it('should track events without attributes', () => {
44
- const metrics = new Metric('test-service', { logger: mockLogger });
45
-
46
- metrics.trackEvent('user.login');
47
-
48
- // Pino-native: (extra, message)
49
- expect(mockLogger.info).toHaveBeenCalledWith(
50
- {
51
- event: 'user.login',
52
- attributes: undefined,
53
- },
54
- 'Metric event tracked',
55
- );
56
- });
57
- });
58
-
59
- describe('trackFunnelStep', () => {
60
- it('should track funnel progression as metrics', () => {
61
- const metrics = new Metric('test-service', { logger: mockLogger });
62
-
63
- metrics.trackFunnelStep('checkout', 'started', { cartValue: 99.99 });
64
- metrics.trackFunnelStep('checkout', 'completed', { cartValue: 99.99 });
65
-
66
- expect(mockLogger.info).toHaveBeenCalledTimes(2);
67
- // Pino-native: (extra, message)
68
- expect(mockLogger.info).toHaveBeenCalledWith(
69
- {
70
- funnel: 'checkout',
71
- status: 'started',
72
- attributes: { cartValue: 99.99 },
73
- },
74
- 'Funnel step tracked',
75
- );
76
- });
77
-
78
- it('should track funnel abandonment', () => {
79
- const metrics = new Metric('test-service', { logger: mockLogger });
80
-
81
- metrics.trackFunnelStep('checkout', 'abandoned', { reason: 'timeout' });
82
-
83
- // Pino-native: (extra, message)
84
- expect(mockLogger.info).toHaveBeenCalledWith(
85
- {
86
- funnel: 'checkout',
87
- status: 'abandoned',
88
- attributes: { reason: 'timeout' },
89
- },
90
- 'Funnel step tracked',
91
- );
92
- });
93
- });
94
-
95
- describe('trackOutcome', () => {
96
- it('should track successful outcomes as metrics', () => {
97
- const metrics = new Metric('test-service', { logger: mockLogger });
98
-
99
- metrics.trackOutcome('payment.process', 'success', {
100
- amount: 99.99,
101
- });
102
-
103
- // Pino-native: (extra, message)
104
- expect(mockLogger.info).toHaveBeenCalledWith(
105
- {
106
- operation: 'payment.process',
107
- status: 'success',
108
- attributes: { amount: 99.99 },
109
- },
110
- 'Outcome tracked',
111
- );
112
- });
113
-
114
- it('should track failed outcomes', () => {
115
- const metrics = new Metric('test-service', { logger: mockLogger });
116
-
117
- metrics.trackOutcome('payment.process', 'failure', {
118
- error: 'insufficient_funds',
119
- });
120
-
121
- // Pino-native: (extra, message)
122
- expect(mockLogger.info).toHaveBeenCalledWith(
123
- {
124
- operation: 'payment.process',
125
- status: 'failure',
126
- attributes: { error: 'insufficient_funds' },
127
- },
128
- 'Outcome tracked',
129
- );
130
- });
131
-
132
- it('should track partial outcomes', () => {
133
- const metrics = new Metric('test-service', { logger: mockLogger });
134
-
135
- metrics.trackOutcome('batch.process', 'partial', {
136
- successCount: 8,
137
- failureCount: 2,
138
- });
139
-
140
- // Pino-native: (extra, message)
141
- expect(mockLogger.info).toHaveBeenCalledWith(
142
- {
143
- operation: 'batch.process',
144
- status: 'partial',
145
- attributes: { successCount: 8, failureCount: 2 },
146
- },
147
- 'Outcome tracked',
148
- );
149
- });
150
- });
151
-
152
- describe('trackValue', () => {
153
- it('should track revenue metrics', () => {
154
- const metrics = new Metric('test-service', { logger: mockLogger });
155
-
156
- metrics.trackValue('order.revenue', 149.99, {
157
- currency: 'USD',
158
- productCategory: 'electronics',
159
- });
160
-
161
- // Pino-native: (extra, message)
162
- expect(mockLogger.debug).toHaveBeenCalledWith(
163
- {
164
- metric: 'order.revenue',
165
- value: 149.99,
166
- attributes: { currency: 'USD', productCategory: 'electronics' },
167
- },
168
- 'Value metric tracked',
169
- );
170
- });
171
-
172
- it('should track processing time', () => {
173
- const metrics = new Metric('test-service', { logger: mockLogger });
174
-
175
- metrics.trackValue('application.processing_time', 2500, {
176
- unit: 'ms',
177
- });
178
-
179
- // Pino-native: (extra, message)
180
- expect(mockLogger.debug).toHaveBeenCalledWith(
181
- {
182
- metric: 'application.processing_time',
183
- value: 2500,
184
- attributes: { unit: 'ms' },
185
- },
186
- 'Value metric tracked',
187
- );
188
- });
189
- });
190
-
191
- describe('getMetrics', () => {
192
- it('should return singleton instance', () => {
193
- const metrics1 = getMetrics('test-service');
194
- const metrics2 = getMetrics('test-service');
195
-
196
- expect(metrics1).toBe(metrics2);
197
- });
198
-
199
- it('should return different instances for different services', () => {
200
- const metrics1 = getMetrics('service-1');
201
- const metrics2 = getMetrics('service-2');
202
-
203
- expect(metrics1).not.toBe(metrics2);
204
- });
205
-
206
- it('should reset instances', () => {
207
- const metrics1 = getMetrics('test-service');
208
- resetMetrics();
209
- const metrics2 = getMetrics('test-service');
210
-
211
- expect(metrics1).not.toBe(metrics2);
212
- });
213
- });
214
-
215
- describe('real-world usage example', () => {
216
- it('should track checkout flow', () => {
217
- const metrics = new Metric('checkout', { logger: mockLogger });
218
-
219
- // User starts checkout
220
- metrics.trackFunnelStep('checkout', 'started', { cartValue: 99.99 });
221
-
222
- // Order completed
223
- metrics.trackEvent('order.completed', {
224
- orderId: 'ord_123',
225
- amount: 99.99,
226
- });
227
- metrics.trackFunnelStep('checkout', 'completed', { cartValue: 99.99 });
228
-
229
- // Payment processed successfully
230
- metrics.trackOutcome('payment.process', 'success', {
231
- amount: 99.99,
232
- });
233
-
234
- // Track revenue
235
- metrics.trackValue('revenue', 99.99, { currency: 'USD' });
236
-
237
- expect(mockLogger.info).toHaveBeenCalledTimes(4);
238
- expect(mockLogger.debug).toHaveBeenCalledTimes(1);
239
- });
240
- });
241
- });
@@ -1,123 +0,0 @@
1
- /**
2
- * Cross-format require() helper for CJS and ESM compatibility
3
- *
4
- * Provides a synchronous `require()` function that works in both:
5
- * - CJS builds: Uses native `require`
6
- * - ESM builds: Uses `createRequire(import.meta.url)`
7
- *
8
- * This allows optional peer dependencies and dynamic module loading
9
- * to work synchronously in both module formats.
10
- */
11
-
12
- import { createRequire } from 'node:module';
13
-
14
- // `__filename` is provided by CJS and by esbuild's CJS output wrapper, but
15
- // is undefined under pure ESM. `import.meta.url` is provided by ESM. Pick
16
- // whichever is available so the helper works in:
17
- // - native CJS (autotel's published `.cjs`)
18
- // - native ESM (autotel's published `.js`)
19
- // - ESM-bundled-into-CJS by a downstream consumer (e.g. CDK's
20
- // `aws-lambda-nodejs` → esbuild with `format: cjs`). esbuild rewrites
21
- // `import.meta` to `{}` in this case, so `createRequire(import.meta.url)`
22
- // alone collapses to `createRequire(undefined)` and crashes at load.
23
- // `typeof __filename` does NOT throw when the identifier is undeclared, so
24
- // the ESM build evaluates the conditional safely.
25
- declare const __filename: string | undefined;
26
-
27
- // Build the Node `require` lazily on first use. Calling `createRequire`
28
- // eagerly at module load crashes in runtimes where neither `__filename` nor
29
- // `import.meta.url` resolves to a path — e.g. Cloudflare Workers / workerd,
30
- // where the bundle has no module path and `createRequire(undefined)` throws
31
- // synchronously at import. Deferring the call keeps merely importing this
32
- // module (and therefore anything that re-exports it, such as `track`)
33
- // side-effect-free; Node/CJS/ESM still get a real `require` on first call.
34
- let cachedRequire: NodeRequire | undefined;
35
-
36
- function getNodeRequire(): NodeRequire {
37
- if (cachedRequire) return cachedRequire;
38
- const base = typeof __filename === 'string' ? __filename : import.meta.url;
39
- if (!base) {
40
- // No module path in this runtime. Surface as a missing-module error so
41
- // optional lookups via `safeRequire()` degrade gracefully to `undefined`.
42
- throw Object.assign(
43
- new Error('node require() is unavailable in this runtime'),
44
- { code: 'MODULE_NOT_FOUND' },
45
- );
46
- }
47
- cachedRequire = createRequire(base);
48
- return cachedRequire;
49
- }
50
-
51
- /**
52
- * Synchronously require a module (works in both CJS and ESM)
53
- *
54
- * @param id - Module ID to require
55
- * @returns The required module
56
- * @throws Error if module cannot be loaded
57
- *
58
- * @example
59
- * ```typescript
60
- * import { safeRequire } from './node-require';
61
- *
62
- * const traceloop = safeRequire('@traceloop/node-server-sdk');
63
- * if (traceloop) {
64
- * traceloop.initialize({ ... });
65
- * }
66
- * ```
67
- */
68
- export function safeRequire<T = unknown>(id: string): T | undefined {
69
- try {
70
- return getNodeRequire()(id) as T;
71
- } catch (error) {
72
- if (error && (error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
73
- // Optional dependency missing – return undefined
74
- return undefined;
75
- }
76
- // Any other error is a real bug: rethrow
77
- throw error;
78
- }
79
- }
80
-
81
- /**
82
- * Synchronously require a module (throws if not found)
83
- *
84
- * Use this when the module is required (not optional).
85
- *
86
- * @param id - Module ID to require
87
- * @returns The required module
88
- * @throws Error if module cannot be loaded
89
- *
90
- * @example
91
- * ```typescript
92
- * import { requireModule } from './node-require';
93
- *
94
- * const fs = requireModule<typeof import('node:fs')>('node:fs');
95
- * const content = fs.readFileSync('file.txt', 'utf8');
96
- * ```
97
- */
98
- export function requireModule<T = unknown>(id: string): T {
99
- return getNodeRequire()(id) as T;
100
- }
101
-
102
- /**
103
- * Direct access to the nodeRequire function (for advanced use cases).
104
- *
105
- * Lazily resolves the underlying Node `require` on first call, so importing
106
- * this binding never triggers `createRequire` in runtimes that lack a module
107
- * path (e.g. Cloudflare Workers).
108
- *
109
- * Only the call signature and `resolve` (including `resolve.paths`) are
110
- * forwarded. The live, mutable members of a real `require` — `.cache`,
111
- * `.main`, `.extensions` — are intentionally NOT exposed: a lazy wrapper
112
- * can't mirror that shared state without resolving eagerly, which would
113
- * reintroduce the workerd crash. Use `createRequire` directly if you need
114
- * them.
115
- */
116
- const nodeRequire = ((id: string) => getNodeRequire()(id)) as NodeRequire;
117
- const lazyResolve = ((id: string, options?: { paths?: string[] }) =>
118
- getNodeRequire().resolve(id, options)) as NodeRequire['resolve'];
119
- lazyResolve.paths = (request: string) =>
120
- getNodeRequire().resolve.paths(request);
121
- nodeRequire.resolve = lazyResolve;
122
-
123
- export { nodeRequire };
@@ -1,93 +0,0 @@
1
- /**
2
- * Operation context tracking using AsyncLocalStorage
3
- *
4
- * This module provides a way to track operation names across async boundaries
5
- * so they can be automatically captured in events events.
6
- *
7
- * We cannot read span attributes from OpenTelemetry's API (it's write-only),
8
- * so we maintain our own async context storage.
9
- */
10
-
11
- import { AsyncLocalStorage } from 'node:async_hooks';
12
-
13
- /**
14
- * Operation context that flows through async operations
15
- */
16
- export interface OperationContext {
17
- /**
18
- * The name of the current operation
19
- * This is set by trace() or span() and can be read by events
20
- */
21
- name: string;
22
- }
23
-
24
- /**
25
- * AsyncLocalStorage instance for tracking operation context
26
- */
27
- const operationStorage = new AsyncLocalStorage<OperationContext>();
28
-
29
- /**
30
- * Get the current operation context (if any)
31
- *
32
- * @returns The current operation context, or undefined if not in an operation
33
- *
34
- * @example
35
- * ```typescript
36
- * const ctx = getOperationContext();
37
- * if (ctx) {
38
- * console.log('Current operation:', ctx.name);
39
- * }
40
- * ```
41
- */
42
- export function getOperationContext(): OperationContext | undefined {
43
- return operationStorage.getStore();
44
- }
45
-
46
- /**
47
- * Run a function within an operation context
48
- *
49
- * This sets the operation name for the duration of the function execution,
50
- * including all async operations spawned from it.
51
- *
52
- * @param name - The operation name to set
53
- * @param fn - The function to execute within the context
54
- * @returns The result of the function
55
- *
56
- * @example
57
- * ```typescript
58
- * const result = await runInOperationContext('user.create', async () => {
59
- * // Any events.trackEvent() calls here will automatically capture
60
- * // 'operation.name': 'user.create'
61
- * await createUser();
62
- * return 'success';
63
- * });
64
- * ```
65
- */
66
- export function runInOperationContext<T>(name: string, fn: () => T): T {
67
- return operationStorage.run({ name }, fn);
68
- }
69
-
70
- /**
71
- * Update the operation name in the current context
72
- *
73
- * This is useful when you want to change the operation name within
74
- * an already-established context (e.g., when entering a nested span).
75
- *
76
- * @param name - The new operation name
77
- *
78
- * @example
79
- * ```typescript
80
- * runInOperationContext('parent.operation', () => {
81
- * // operation.name is 'parent.operation'
82
- *
83
- * updateOperationName('nested.operation');
84
- * // operation.name is now 'nested.operation'
85
- * });
86
- * ```
87
- */
88
- export function updateOperationName(name: string): void {
89
- const store = operationStorage.getStore();
90
- if (store) {
91
- store.name = name;
92
- }
93
- }
@@ -1,73 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { createStructuredError } from './structured-error';
3
- import { parseError } from './parse-error';
4
-
5
- describe('parseError', () => {
6
- it('parses structured errors with why/fix/link', () => {
7
- const err = createStructuredError({
8
- message: 'Payment failed',
9
- status: 402,
10
- why: 'Card declined',
11
- fix: 'Use another card',
12
- link: 'https://docs.example.com/payment-errors',
13
- code: 'PAYMENT_DECLINED',
14
- });
15
-
16
- const parsed = parseError(err);
17
- expect(parsed).toMatchObject({
18
- message: 'Payment failed',
19
- status: 402,
20
- why: 'Card declined',
21
- fix: 'Use another card',
22
- link: 'https://docs.example.com/payment-errors',
23
- code: 'PAYMENT_DECLINED',
24
- raw: err,
25
- });
26
- });
27
-
28
- it('parses fetch-like nested data payloads', () => {
29
- const fetchLikeError = {
30
- message: 'Request failed',
31
- statusCode: 409,
32
- data: {
33
- statusText: 'Conflict',
34
- data: {
35
- why: 'Order already exists',
36
- fix: 'Use idempotency key',
37
- link: 'https://docs.example.com/idempotency',
38
- code: 'ORDER_EXISTS',
39
- },
40
- },
41
- };
42
-
43
- const parsed = parseError(fetchLikeError);
44
- expect(parsed).toMatchObject({
45
- message: 'Conflict',
46
- status: 409,
47
- why: 'Order already exists',
48
- fix: 'Use idempotency key',
49
- link: 'https://docs.example.com/idempotency',
50
- code: 'ORDER_EXISTS',
51
- raw: fetchLikeError,
52
- });
53
- });
54
-
55
- it('preserves details from structured errors', () => {
56
- const err = createStructuredError({
57
- message: 'Export failed',
58
- status: 500,
59
- details: { retryable: true, provider: 'stripe' },
60
- });
61
-
62
- const parsed = parseError(err);
63
- expect(parsed.details).toEqual({ retryable: true, provider: 'stripe' });
64
- });
65
-
66
- it('falls back for unknown values', () => {
67
- expect(parseError('boom')).toMatchObject({
68
- message: 'boom',
69
- status: 500,
70
- raw: 'boom',
71
- });
72
- });
73
- });
@@ -1,112 +0,0 @@
1
- import type { StructuredError } from './structured-error';
2
-
3
- export interface ParsedError {
4
- message: string;
5
- status: number;
6
- why?: string;
7
- fix?: string;
8
- link?: string;
9
- code?: string | number;
10
- details?: Record<string, unknown>;
11
- raw: unknown;
12
- }
13
-
14
- type ErrorLike = {
15
- message?: unknown;
16
- status?: unknown;
17
- statusCode?: unknown;
18
- data?: unknown;
19
- code?: unknown;
20
- why?: unknown;
21
- fix?: unknown;
22
- link?: unknown;
23
- details?: unknown;
24
- };
25
-
26
- function toStatus(value: unknown): number | undefined {
27
- if (typeof value === 'number' && Number.isFinite(value)) return value;
28
- if (typeof value === 'string') {
29
- const n = Number(value);
30
- if (Number.isFinite(n)) return n;
31
- }
32
- return undefined;
33
- }
34
-
35
- function pickString(value: unknown): string | undefined {
36
- return typeof value === 'string' && value.length > 0 ? value : undefined;
37
- }
38
-
39
- function pickCode(value: unknown): string | number | undefined {
40
- if (typeof value === 'string' || typeof value === 'number') return value;
41
- return undefined;
42
- }
43
-
44
- function pickDetails(value: unknown): Record<string, unknown> | undefined {
45
- if (value && typeof value === 'object' && value.constructor === Object) {
46
- return value as Record<string, unknown>;
47
- }
48
- return undefined;
49
- }
50
-
51
- export function parseError(error: unknown): ParsedError {
52
- if (error instanceof Error) {
53
- const structured = error as StructuredError;
54
- return {
55
- message: error.message || 'An error occurred',
56
- status: toStatus(structured.status) ?? 500,
57
- why: pickString(structured.why),
58
- fix: pickString(structured.fix),
59
- link: pickString(structured.link),
60
- code: pickCode(structured.code),
61
- details: pickDetails(structured.details),
62
- raw: error,
63
- };
64
- }
65
-
66
- if (error && typeof error === 'object') {
67
- const err = error as ErrorLike;
68
- const data =
69
- err.data && typeof err.data === 'object'
70
- ? (err.data as Record<string, unknown>)
71
- : undefined;
72
- const nested =
73
- data?.data && typeof data.data === 'object'
74
- ? (data.data as Record<string, unknown>)
75
- : undefined;
76
- const payload = nested ?? data;
77
-
78
- const message =
79
- pickString(data?.statusText) ||
80
- pickString(data?.statusMessage) ||
81
- pickString(data?.message) ||
82
- pickString(payload?.statusText) ||
83
- pickString(payload?.statusMessage) ||
84
- pickString(payload?.message) ||
85
- pickString(err.message) ||
86
- 'An error occurred';
87
-
88
- const status =
89
- toStatus(payload?.status) ||
90
- toStatus(payload?.statusCode) ||
91
- toStatus(err.status) ||
92
- toStatus(err.statusCode) ||
93
- 500;
94
-
95
- return {
96
- message,
97
- status,
98
- why: pickString(payload?.why) || pickString(err.why),
99
- fix: pickString(payload?.fix) || pickString(err.fix),
100
- link: pickString(payload?.link) || pickString(err.link),
101
- code: pickCode(payload?.code) || pickCode(err.code),
102
- details: pickDetails(payload?.details) || pickDetails(err.details),
103
- raw: error,
104
- };
105
- }
106
-
107
- return {
108
- message: String(error),
109
- status: 500,
110
- raw: error,
111
- };
112
- }