@djangocfg/devtools 2.1.666 → 2.1.668

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/devtools",
3
- "version": "2.1.666",
3
+ "version": "2.1.668",
4
4
  "description": "Frontend devtools for django-cfg apps: error/console capture with backend ingest, plus a floating debug panel. One package, one event stream.",
5
5
  "keywords": [
6
6
  "django",
@@ -51,18 +51,18 @@
51
51
  "test:watch": "vitest"
52
52
  },
53
53
  "peerDependencies": {
54
- "@djangocfg/api": "^2.1.666",
55
- "@djangocfg/ui-core": "^2.1.666",
54
+ "@djangocfg/api": "^2.1.668",
55
+ "@djangocfg/ui-core": "^2.1.668",
56
56
  "lucide-react": ">=0.400.0",
57
57
  "react": "^19.0.0",
58
58
  "react-dom": "^19.0.0",
59
59
  "zustand": "^5.0.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@djangocfg/api": "^2.1.666",
63
- "@djangocfg/eslint-config": "^2.1.666",
64
- "@djangocfg/typescript-config": "^2.1.666",
65
- "@djangocfg/ui-core": "^2.1.666",
62
+ "@djangocfg/api": "^2.1.668",
63
+ "@djangocfg/eslint-config": "^2.1.668",
64
+ "@djangocfg/typescript-config": "^2.1.668",
65
+ "@djangocfg/ui-core": "^2.1.668",
66
66
  "@storybook/react-vite": "^10.6.5",
67
67
  "@types/node": "^25.9.5",
68
68
  "@types/react": "19.3.0",
@@ -0,0 +1,82 @@
1
+ // @vitest-environment jsdom
2
+ import { afterEach, describe, expect, it, vi } from 'vitest';
3
+
4
+ const { capture } = vi.hoisted(() => ({ capture: vi.fn() }));
5
+ vi.mock('../store', () => ({ devtoolsStore: { getState: () => ({ capture }) } }));
6
+
7
+ import { installConsoleCapture, installValidationCapture, installNetworkCapture } from '../capture';
8
+
9
+ describe('generated-client validation reporting', () => {
10
+ afterEach(() => {
11
+ capture.mockClear();
12
+ vi.restoreAllMocks();
13
+ });
14
+
15
+ it('keeps the console diagnostic visible and ingests the structured event once', () => {
16
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
17
+ const stopConsole = installConsoleCapture();
18
+ const stopValidation = installValidationCapture();
19
+ try {
20
+ console.error('[DjangoCFG:validation] ERR-12345 GET /apix/test/: slug: invalid_format');
21
+ expect(consoleSpy).toHaveBeenCalledTimes(1);
22
+ expect(capture).not.toHaveBeenCalled();
23
+
24
+ window.dispatchEvent(new CustomEvent('zod-validation-error', {
25
+ detail: {
26
+ operation: 'GET /apix/test/', method: 'GET', path: '/apix/test/',
27
+ reference: 'ERR-12345',
28
+ diagnostic: 'slug: invalid_format',
29
+ error: { message: 'private raw response value' },
30
+ },
31
+ }));
32
+ expect(capture).toHaveBeenCalledTimes(1);
33
+ expect(capture.mock.calls[0]?.[0]).toMatchObject({
34
+ http_method: 'GET', http_url: '/apix/test/',
35
+ extra: { reference: 'ERR-12345' },
36
+ });
37
+ expect(capture.mock.calls[0]?.[0].message).toContain('slug: invalid_format');
38
+ expect(JSON.stringify(capture.mock.calls[0])).not.toContain('private raw response value');
39
+ } finally {
40
+ stopValidation();
41
+ stopConsole();
42
+ }
43
+ });
44
+ });
45
+
46
+ describe('generated-client network reporting', () => {
47
+ afterEach(() => {
48
+ capture.mockClear();
49
+ vi.restoreAllMocks();
50
+ });
51
+
52
+ it('does not ingest console-only refusals and captures a network event once', () => {
53
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
54
+ const error = vi.spyOn(console, 'error').mockImplementation(() => {});
55
+ const stopConsole = installConsoleCapture();
56
+ const stopNetwork = installNetworkCapture();
57
+ try {
58
+ console.warn('[DjangoCFG:request] ERR-ONE POST /apix/test/: HTTP 400');
59
+ console.error('[DjangoCFG:request] ERR-TWO GET /apix/test/: HTTP 503');
60
+ expect(warn).toHaveBeenCalledTimes(1);
61
+ expect(error).toHaveBeenCalledTimes(1);
62
+ expect(capture).not.toHaveBeenCalled();
63
+
64
+ window.dispatchEvent(new CustomEvent('network-error', {
65
+ detail: { method: 'GET', url: '/apix/test/', error: 'HTTP 503',
66
+ statusCode: 503, reference: 'ERR-TWO' },
67
+ }));
68
+ expect(capture).toHaveBeenCalledTimes(1);
69
+ expect(capture.mock.calls[0]?.[0]).toMatchObject({
70
+ http_method: 'GET', http_url: '/apix/test/', http_status: 503,
71
+ extra: { reference: 'ERR-TWO' },
72
+ });
73
+ window.dispatchEvent(new CustomEvent('network-error', {
74
+ detail: { method: 'POST', url: '/cfg/monitor/ingest/', error: 'Network request failed' },
75
+ }));
76
+ expect(capture).toHaveBeenCalledTimes(1);
77
+ } finally {
78
+ stopNetwork();
79
+ stopConsole();
80
+ }
81
+ });
82
+ });
package/src/capture.ts CHANGED
@@ -179,6 +179,9 @@ export function installConsoleCapture(): () => void {
179
179
 
180
180
  const report = (level: 'warn' | 'error', args: unknown[]) => {
181
181
  try {
182
+ // Generated-client diagnostics already have an event owner. Routine
183
+ // HTTP 4xx are console-only, so do not turn them into monitor alerts.
184
+ if (typeof args[0] === 'string' && /^\[DjangoCFG:(validation|request)\] /.test(args[0])) return
182
185
  const message = stringifyArgs(args)
183
186
  const stack = args.find((a): a is Error => a instanceof Error)?.stack
184
187
  if (isNoise(message, stack)) return
@@ -217,6 +220,8 @@ interface ValidationErrorDetail {
217
220
  path: string
218
221
  method: string
219
222
  error: { message: string }
223
+ reference?: string
224
+ diagnostic?: string
220
225
  }
221
226
 
222
227
  export function installValidationCapture(): () => void {
@@ -230,12 +235,12 @@ export function installValidationCapture(): () => void {
230
235
  event_type: EventType.WARNING,
231
236
  level: EventLevel.WARNING,
232
237
  message: truncate(
233
- `Zod validation error in ${detail.operation}: ${detail.error?.message ?? 'unknown'}`,
238
+ `Zod validation error in ${detail.operation}: ${detail.diagnostic ?? detail.error?.message ?? 'unknown'}`,
234
239
  MSG_MAX,
235
240
  ),
236
241
  http_method: detail.method,
237
242
  http_url: detail.path,
238
- extra: { operation: detail.operation, path: detail.path, method: detail.method },
243
+ extra: { operation: detail.operation, path: detail.path, method: detail.method, ...(detail.reference && { reference: detail.reference }) },
239
244
  ...browserContext(),
240
245
  })
241
246
  } catch {
@@ -246,3 +251,40 @@ export function installValidationCapture(): () => void {
246
251
  window.addEventListener('zod-validation-error', handler)
247
252
  return () => window.removeEventListener('zod-validation-error', handler)
248
253
  }
254
+
255
+ // ─── network-error events (unexpected generated-client request failures) ────
256
+
257
+ interface NetworkErrorDetail {
258
+ method: string
259
+ url: string
260
+ error: string
261
+ statusCode?: number
262
+ possibly_cors?: boolean
263
+ reference?: string
264
+ }
265
+
266
+ export function installNetworkCapture(): () => void {
267
+ if (typeof window === 'undefined') return () => {}
268
+ const handler = (event: Event) => {
269
+ if (!(event instanceof CustomEvent)) return
270
+ try {
271
+ const detail = event.detail as NetworkErrorDetail
272
+ if (INGEST_PATTERN.test(detail.url)) return
273
+ devtoolsStore.getState().capture({
274
+ event_type: EventType.NETWORK_ERROR,
275
+ level: detail.statusCode && detail.statusCode >= 400 && detail.statusCode < 500 ? EventLevel.WARNING : EventLevel.ERROR,
276
+ message: truncate(`${detail.method} ${detail.url}: ${detail.error}`, MSG_MAX),
277
+ http_method: detail.method,
278
+ http_url: detail.url,
279
+ ...(typeof detail.statusCode === 'number' && { http_status: detail.statusCode }),
280
+ extra: {
281
+ ...(detail.reference && { reference: detail.reference }),
282
+ ...(detail.possibly_cors && { possibly_cors: true }),
283
+ },
284
+ ...browserContext(),
285
+ })
286
+ } catch { /* never crash */ }
287
+ }
288
+ window.addEventListener('network-error', handler)
289
+ return () => window.removeEventListener('network-error', handler)
290
+ }
package/src/instance.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  installConsoleCapture,
13
13
  installJsErrorCapture,
14
14
  installValidationCapture,
15
+ installNetworkCapture,
15
16
  } from './capture'
16
17
  import { DEFAULT_FLUSH_INTERVAL, getSessionId, isDevelopment, rawConsole } from './internal'
17
18
  import { devtoolsStore } from './store'
@@ -82,7 +83,7 @@ export const devtools = {
82
83
 
83
84
  if (resolved.captureJsErrors !== false) cleanups.push(installJsErrorCapture())
84
85
  if (resolved.captureConsole !== false) cleanups.push(installConsoleCapture())
85
- cleanups.push(installValidationCapture())
86
+ cleanups.push(installValidationCapture(), installNetworkCapture())
86
87
 
87
88
  flushTimer = setInterval(
88
89
  () => devtoolsStore.getState().flush(),