diff --git a/api/docs/ERROR_MONITORING_PUBLIC_API.md b/api/docs/ERROR_MONITORING_PUBLIC_API.md index 3d99337..39334dc 100644 --- a/api/docs/ERROR_MONITORING_PUBLIC_API.md +++ b/api/docs/ERROR_MONITORING_PUBLIC_API.md @@ -51,6 +51,8 @@ Alias: `GET /v1/paperclip/recent-errors` Each row includes CRM-compatible fields. For non-resolved incidents, `status` is always `"open"` (including `investigating`) so Paperclip `error_loop` continues to treat rows as open. +**`stack_trace`:** Non-empty for every row: missing client stacks are synthesized on ingest; legacy DB rows with null/blank `stack_trace` get the same synthesis when serializing this endpoint (BIT-1008). + **Example** ```bash diff --git a/api/src/lib/synthesizeStackTraceForDisplay.test.ts b/api/src/lib/synthesizeStackTraceForDisplay.test.ts new file mode 100644 index 0000000..9393ecc --- /dev/null +++ b/api/src/lib/synthesizeStackTraceForDisplay.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest'; +import { synthesizeStackTraceForDisplay } from './synthesizeStackTraceForDisplay.js'; + +describe('synthesizeStackTraceForDisplay', () => { + it('uses message first line when context is null', () => { + expect(synthesizeStackTraceForDisplay('Error: boom\n#0 x', null)).toBe( + '[synthesized: no stack trace in ingest payload]\nError: boom', + ); + }); + + it('adds file and line from context', () => { + expect( + synthesizeStackTraceForDisplay('oops', { + file_path: '/app/Foo.php', + line_number: '12', + }), + ).toBe('[synthesized: no stack trace in ingest payload]\n#0 /app/Foo.php:12: oops'); + }); + + it('matches ingest-api wording for filename alias', () => { + expect(synthesizeStackTraceForDisplay('x', { filename: '/b.php', line: '3' })).toBe( + '[synthesized: no stack trace in ingest payload]\n#0 /b.php:3: x', + ); + }); +}); diff --git a/api/src/lib/synthesizeStackTraceForDisplay.ts b/api/src/lib/synthesizeStackTraceForDisplay.ts new file mode 100644 index 0000000..f07642e --- /dev/null +++ b/api/src/lib/synthesizeStackTraceForDisplay.ts @@ -0,0 +1,30 @@ +/** + * When `er_incident_occurrences.stack_trace` is NULL or blank (legacy rows or + * reporters that omitted stacks before hub synthesis), build a non-empty + * `stack_trace` for `GET …/recent-errors` so CRM / Paperclip consumers always + * receive a stable contract (BIT-1008, ERRPAYLOAD:5d26a50c83c0). + * + * Keep byte-for-byte aligned with + * `ingest-api/src/lib/ingestStacktrace.ts` → `synthesizeStackTraceForOccurrence`. + */ + +const _SYNTH_HEAD = '[synthesized: no stack trace in ingest payload]'; + +export function synthesizeStackTraceForDisplay( + message: string, + context: Record | null, +): string { + const firstLine = message.split(/\r?\n/)[0]?.trim() ?? message; + if (!context) { + return `${_SYNTH_HEAD}\n${firstLine}`; + } + const fp = String(context.file_path ?? context.filename ?? context.file ?? '').trim(); + const ln = String(context.line_number ?? context.line ?? context.lineno ?? '').trim(); + if (fp !== '' && ln !== '') { + return `${_SYNTH_HEAD}\n#0 ${fp}:${ln}: ${firstLine}`; + } + if (fp !== '') { + return `${_SYNTH_HEAD}\n#0 ${fp}: ${firstLine}`; + } + return `${_SYNTH_HEAD}\n${firstLine}`; +} diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index 31df78e..ff15abc 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -18,6 +18,7 @@ import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; import type { RowDataPacket } from 'mysql2'; import { authorizeReadQuery } from '../../lib/errorMonitoringAuth.js'; +import { synthesizeStackTraceForDisplay } from '../../lib/synthesizeStackTraceForDisplay.js'; const INCIDENT_STATES = ['open', 'investigating', 'resolved', 'ignored'] as const; const SEVERITIES = ['critical', 'high', 'medium', 'low', 'info'] as const; @@ -185,12 +186,19 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): const incSev = String(row.incident_severity ?? 'medium').toLowerCase(); const sev = ['critical', 'high', 'medium', 'low'].includes(incSev) ? incSev : 'medium'; const st = String(row.incident_state ?? 'open').toLowerCase(); + const message = String(row.message ?? ''); + const rawStack = + row.stack_trace != null && String(row.stack_trace).trim() !== '' + ? String(row.stack_trace).trim() + : ''; + const stack_trace = + rawStack !== '' ? rawStack : synthesizeStackTraceForDisplay(message, ctx); return { status: st === 'resolved' ? 'resolved' : st === 'ignored' ? 'ignored' : 'open', - message: String(row.message ?? ''), - error_message: String(row.message ?? ''), - stack_trace: row.stack_trace != null ? String(row.stack_trace) : '', + message, + error_message: message, + stack_trace, file_path, line_number, severity: sev, diff --git a/docs/HTTP_CONTRACTS.md b/docs/HTTP_CONTRACTS.md index 542d9d7..e1e92cb 100644 --- a/docs/HTTP_CONTRACTS.md +++ b/docs/HTTP_CONTRACTS.md @@ -58,7 +58,7 @@ Alias: `GET /v1/paperclip/recent-errors` (same handler). "status": "open", "message": "Exception: …", "error_message": "Exception: …", - "stack_trace": "", + "stack_trace": "#0 /app/src/foo.ts:42: Exception: …", "file_path": "/app/src/foo.ts", "line_number": "42", "severity": "high", @@ -70,6 +70,8 @@ Alias: `GET /v1/paperclip/recent-errors` (same handler). } ``` +**`stack_trace`:** The hub returns a **non-empty** string for every row. Ingest stores a synthesized stack when the reporter omits one; for legacy occurrences whose column is still null/blank, `recent-errors` applies the same synthesis at read time using `message` and `context_json` (BIT-1008). + ```bash API_KEY="$ERROR_RESOLVE_API_KEY" BASE="${ER_API_BASE:-http://127.0.0.1:3200}"