fix(api): non-empty stack_trace on recent-errors for legacy rows

When occurrence.stack_trace is null/blank, synthesize the same text as
ingest-api (BIT-796) at read time so CRM / Paperclip always get a stable
contract (BIT-1008, ERRPAYLOAD:5d26a50c83c0). Add unit tests and document
in HTTP_CONTRACTS + ERROR_MONITORING_PUBLIC_API.

Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376
Paperclip-Issue: BIT-1008
Paperclip-Run: 4c4da0dd-38ab-4f32-86d7-f2502c7cceca
Made-with: Cursor
This commit is contained in:
Peter Spark committed 2026-04-05 10:49:24 +00:00
1 parent eaa326e2bb
commit 6971b7669c
5 files changed
+71 -4

No files matched your search

@@ -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',
);
});
});
@@ -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<string, unknown> | 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}`;
}