From bc8bd8707f77c949cd7a2a128070b339daf4002f Mon Sep 17 00:00:00 2001 From: David Santos Date: Wed, 1 Apr 2026 16:28:43 +0000 Subject: [PATCH] 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 --- .../error-monitoring/recent-errors/route.ts | 2 +- api/.env.example | 2 +- api/src/routes/public/recentErrors.ts | 87 ++++++++++--------- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/admin-ui/app/api/error-monitoring/recent-errors/route.ts b/admin-ui/app/api/error-monitoring/recent-errors/route.ts index 3f32704..300b59a 100644 --- a/admin-ui/app/api/error-monitoring/recent-errors/route.ts +++ b/admin-ui/app/api/error-monitoring/recent-errors/route.ts @@ -21,7 +21,7 @@ export async function GET(request: NextRequest) { } const base = rawBase.replace(/\/$/, ""); const src = new URL(request.url); - const target = `${base}/v1/paperclip/recent-errors${src.search}`; + const target = `${base}/api/error-monitoring/recent-errors${src.search}`; const upstream = await fetch(target, { method: "GET", diff --git a/api/.env.example b/api/.env.example index 76223b4..3949459 100644 --- a/api/.env.example +++ b/api/.env.example @@ -15,7 +15,7 @@ JWT_SECRET=change_me_in_production_64_bytes_minimum # Cookie signing — generate with: openssl rand -base64 32 COOKIE_SECRET=change_me_in_production_32_bytes_minimum -# Recent-errors for Paperclip (optional — if unset, GET /v1/paperclip/recent-errors returns 503) +# Recent-errors for Paperclip (optional — if unset, GET …/recent-errors returns 503 on both CRM paths) # ERROR_RESOLVE_API_KEY=same_as_CRM_or_shared_hub_secret # DEBUG_VIEW_ERRORS_KEY=read_only_alternate_query_param_view_errors diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index e8c27d7..e0e8be8 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -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 | null): { file_path: stri return { file_path, line_number }; } -export default async function recentErrorsPublicRoutes(app: FastifyInstance): Promise { - const db = app.db; +async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): Promise { + 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(sql, [limit]); + const [rows] = await db.execute(sql, [limit]); - const data = (rows as Record[]).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[]).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 { + app.get('/api/error-monitoring/recent-errors', handleRecentErrors); + app.get('/v1/paperclip/recent-errors', handleRecentErrors); +}