diff --git a/src/browser/routes/agent.shared.ts b/src/browser/routes/agent.shared.ts index 37b30e00a50..5be92f9c0c2 100644 --- a/src/browser/routes/agent.shared.ts +++ b/src/browser/routes/agent.shared.ts @@ -133,11 +133,18 @@ export async function withRouteTabContext( return params.res; }; - const result = await params.run({ - profileCtx, - tab, - cdpUrl: profileCtx.profile.cdpUrl, - }); + let result: T | undefined; + try { + result = await params.run({ + profileCtx, + tab, + cdpUrl: profileCtx.profile.cdpUrl, + }); + } catch (runErr) { + // Restore original res.json so error handling can actually send. + params.res.json = originalJson; + throw runErr; + } // Now enrich and flush the intercepted response body. if (jsonCalled) {