feat(api): expose CRM-shaped GET /api/error-monitoring/recent-errors on Fastify

Register the same handler as /v1/paperclip/recent-errors so NPM and direct
API probes (e.g. :3200) match Paperclip default PAPERCLIP_ERROR_API_QUERY.
Admin UI proxy now calls the Fastify path for a single contract end-to-end.

Paperclip-Agent: David Santos (urlKey david-santos; id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376)
Paperclip-Issue: BIT-702
Paperclip-Run: 4ba525c4-3929-475c-a845-400c49eda817
Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-01 16:28:43 +00:00
1 parent 6811dba303
commit bc8bd8707f
3 files changed
+48 -43

No files matched your search

+46 -41
View File
@@ -1,6 +1,8 @@
/**
* Paperclip / CRM-compatible read-only listing for open incidents.
* GET /v1/paperclip/recent-errors?limit=&api_key=|view_errors=
* Paths (same handler, Paperclip default query is the CRM-shaped one):
* GET /api/error-monitoring/recent-errors?limit=&api_key=|view_errors=
* GET /v1/paperclip/recent-errors?limit=&api_key=|view_errors=
*
* Auth: query param must match ERROR_RESOLVE_API_KEY or ERROR_REPORTING_API_KEY (api_key),
* or DEBUG_VIEW_ERRORS_KEY (view_errors). At least one server secret must be configured.
@@ -69,25 +71,24 @@ function extractFileLine(ctx: Record<string, unknown> | null): { file_path: stri
return { file_path, line_number };
}
export default async function recentErrorsPublicRoutes(app: FastifyInstance): Promise<void> {
const db = app.db;
async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): Promise<void> {
const db = request.server.db;
app.get('/v1/paperclip/recent-errors', async (request, reply) => {
if (!authorizeQuery(request, reply)) {
return;
if (!authorizeQuery(request, reply)) {
return;
}
const q = request.query as { limit?: string };
let limit = 999999;
if (q.limit != null && q.limit !== '') {
const n = parseInt(String(q.limit), 10);
if (!Number.isFinite(n) || n < 1) {
return void reply.code(400).send({ success: false, message: 'Invalid limit' });
}
limit = Math.min(n, 999999);
}
const q = request.query as { limit?: string };
let limit = 999999;
if (q.limit != null && q.limit !== '') {
const n = parseInt(String(q.limit), 10);
if (!Number.isFinite(n) || n < 1) {
return reply.code(400).send({ success: false, message: 'Invalid limit' });
}
limit = Math.min(n, 999999);
}
const sql = `
const sql = `
WITH ranked AS (
SELECT o.id,
o.incident_id,
@@ -118,32 +119,36 @@ export default async function recentErrorsPublicRoutes(app: FastifyInstance): Pr
LIMIT ?
`;
const [rows] = await db.execute<RowDataPacket[]>(sql, [limit]);
const [rows] = await db.execute<RowDataPacket[]>(sql, [limit]);
const data = (rows as Record<string, unknown>[]).map((row) => {
const ctx = parseJsonObject(row.context_json != null ? String(row.context_json) : null);
const { file_path, line_number } = extractFileLine(ctx);
const incSev = String(row.incident_severity ?? 'medium').toLowerCase();
const sev = ['critical', 'high', 'medium', 'low'].includes(incSev) ? incSev : 'medium';
const data = (rows as Record<string, unknown>[]).map((row) => {
const ctx = parseJsonObject(row.context_json != null ? String(row.context_json) : null);
const { file_path, line_number } = extractFileLine(ctx);
const incSev = String(row.incident_severity ?? 'medium').toLowerCase();
const sev = ['critical', 'high', 'medium', 'low'].includes(incSev) ? incSev : 'medium';
return {
status: 'open',
message: String(row.message ?? ''),
error_message: String(row.message ?? ''),
stack_trace: row.stack_trace != null ? String(row.stack_trace) : '',
file_path,
line_number,
severity: sev,
unique_id: String(row.occurrence_id ?? row.incident_id ?? ''),
project_slug: String(row.project_slug ?? '').toLowerCase(),
environment: String(row.environment ?? 'production'),
};
});
return {
status: 'open',
message: String(row.message ?? ''),
error_message: String(row.message ?? ''),
stack_trace: row.stack_trace != null ? String(row.stack_trace) : '',
file_path,
line_number,
severity: sev,
unique_id: String(row.occurrence_id ?? row.incident_id ?? ''),
project_slug: String(row.project_slug ?? '').toLowerCase(),
environment: String(row.environment ?? 'production'),
};
});
return reply.send({
success: true,
message: 'ok',
data,
});
return void reply.send({
success: true,
message: 'ok',
data,
});
}
export default async function recentErrorsPublicRoutes(app: FastifyInstance): Promise<void> {
app.get('/api/error-monitoring/recent-errors', handleRecentErrors);
app.get('/v1/paperclip/recent-errors', handleRecentErrors);
}