Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): map research connect timeouts to 504

Karan Lokchandanie85549351184
diff --git a/apps/api/src/controllers/v2/__tests__/research-timeouts.test.ts b/apps/api/src/controllers/v2/__tests__/research-timeouts.test.ts
index 6a1139c3f..7cb2f1766 100644
--- a/apps/api/src/controllers/v2/__tests__/research-timeouts.test.ts
+++ b/apps/api/src/controllers/v2/__tests__/research-timeouts.test.ts
@@ -159,13 +159,22 @@ describe("research upstream timeouts", () => {
);
});
- it("maps an operation timeout to 504", async () => {
- mocks.fetchResearchUpstream.mockRejectedValue(
- new DOMException(
+ it.each([
+ {
+ name: "request",
+ error: new DOMException(
"The operation was aborted due to timeout",
"TimeoutError",
),
- );
+ },
+ {
+ name: "connect",
+ error: Object.assign(new Error("Connect Timeout Error"), {
+ name: "ConnectTimeoutError",
+ }),
+ },
+ ])("maps a $name timeout to 504", async ({ error }) => {
+ mocks.fetchResearchUpstream.mockRejectedValue(error);
const res = await callRoute(createResearchRouter(), "/github", {
query: "retry logic",
diff --git a/apps/api/src/controllers/v2/research-proxy.ts b/apps/api/src/controllers/v2/research-proxy.ts
index 1c3102200..1da8da006 100644
--- a/apps/api/src/controllers/v2/research-proxy.ts
+++ b/apps/api/src/controllers/v2/research-proxy.ts
@@ -243,6 +243,13 @@ function researchError(
});
}
+function isResearchTimeoutError(error: unknown): boolean {
+ return (
+ (error instanceof DOMException && error.name === "TimeoutError") ||
+ (error instanceof Error && error.name === "ConnectTimeoutError")
+ );
+}
+
async function fetchForRequest(
req: RequestWithAuth<any, any, any>,
path: string,
@@ -382,7 +389,7 @@ function createResearchController(
: responseBody;
return res.status(statusCode).json(response);
} catch (err: unknown) {
- if (err instanceof DOMException && err.name === "TimeoutError") {
+ if (isResearchTimeoutError(err)) {
statusCode = 504;
error = "Research service timed out";
return res.status(504).end();