diff --git a/src/browser/routes/agent.shared.ts b/src/browser/routes/agent.shared.ts index 5be92f9c0c2..33b15d23b45 100644 --- a/src/browser/routes/agent.shared.ts +++ b/src/browser/routes/agent.shared.ts @@ -183,11 +183,18 @@ export async function withRouteTabContext( } } } + // Restore res.json before flushing so that if originalJson throws + // (e.g. BigInt serialization), the outer catch can still send errors. + params.res.json = originalJson; originalJson(interceptedBody); } return result; } catch (err) { + // Ensure res.json is always restored for error handling. + if (params.res.json !== originalJson) { + params.res.json = originalJson; + } handleRouteError(params.ctx, params.res, err); return undefined; }