Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(api): handle EAI_AGAIN DNS lookup failures with HTTP 503 in StorageService (#9743)

Copilot64a7338dd742
diff --git a/packages/shared/src/errors/ServiceUnavailableError.ts b/packages/shared/src/errors/ServiceUnavailableError.ts
new file mode 100644
index 0000000000..299fdad07d
--- /dev/null
+++ b/packages/shared/src/errors/ServiceUnavailableError.ts
@@ -0,0 +1,7 @@
+import { BaseError } from "./BaseError";
+
+export class ServiceUnavailableError extends BaseError {
+ constructor(description = "Service Temporarily Unavailable") {
+ super("ServiceUnavailableError", 503, description, true);
+ }
+}
diff --git a/packages/shared/src/errors/index.ts b/packages/shared/src/errors/index.ts
index 0f2a26776a..56d5ec6618 100644
--- a/packages/shared/src/errors/index.ts
+++ b/packages/shared/src/errors/index.ts
@@ -7,4 +7,5 @@ export { MethodNotAllowedError } from "./MethodNotAllowedError";
export { ApiError } from "./ApiError";
export { InternalServerError } from "./InternalServerError";
export { LangfuseConflictError } from "./ConflictError";
+export { ServiceUnavailableError } from "./ServiceUnavailableError";
export { QUEUE_ERROR_MESSAGES } from "./utils/constants";
diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.ts
index 6f30569840..e84eb813b1 100644
--- a/packages/shared/src/server/services/StorageService.ts
+++ b/packages/shared/src/server/services/StorageService.ts
@@ -19,6 +19,7 @@ import { Storage, Bucket, GetSignedUrlConfig } from "@google-cloud/storage";
import { logger } from "../logger";
import { env } from "../../env";
import { backOff } from "exponential-backoff";
+import { ServiceUnavailableError } from "../../errors";
type UploadFile = {
fileName: string;
@@ -30,6 +31,27 @@ type UploadWithSignedUrl = UploadFile & {
expiresInSeconds: number;
};
+/**
+ * Check if an error is a DNS lookup failure (EAI_AGAIN)
+ * and throw ServiceUnavailableError if so, otherwise rethrow the original error
+ */
+function handleStorageError(err: unknown, operation: string): never {
+ // Check if error has a code property matching EAI_AGAIN
+ if (
+ err &&
+ typeof err === "object" &&
+ "code" in err &&
+ err.code === "EAI_AGAIN"
+ ) {
+ logger.error(`DNS lookup failure during ${operation}`, err);
+ throw new ServiceUnavailableError(
+ "Storage service temporarily unavailable due to network issues",
+ );
+ }
+ // For other errors, throw a generic error
+ throw Error(`Failed to ${operation}`);
+}
+
export interface StorageService {
uploadFile(params: UploadFile): Promise<void>; // eslint-disable-line no-unused-vars
@@ -164,7 +186,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to create Azure Blob Storage container ${this.container}`,
err,
);
- throw Error("Failed to create Azure Blob Storage container ");
+ handleStorageError(err, "create Azure Blob Storage container");
}
}
@@ -209,7 +231,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to upload file to Azure Blob Storage ${fileName}`,
err,
);
- throw Error("Failed to upload file to Azure Blob Storage");
+ handleStorageError(err, "upload file to Azure Blob Storage");
}
}
@@ -228,7 +250,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to upload file to Azure Blob Storage ${fileName}`,
err,
);
- throw Error("Failed to upload file to Azure Blob Storage");
+ handleStorageError(err, "upload file to Azure Blob Storage");
}
}
@@ -244,7 +266,7 @@ class AzureBlobStorageService implements StorageService {
await blockBlobClient.upload(content, content.length);
} catch (err) {
logger.error(`Failed to upload JSON to Azure Blob Storage ${path}`, err);
- throw Error("Failed to upload JSON to Azure Blob Storage");
+ handleStorageError(err, "upload JSON to Azure Blob Storage");
}
}
@@ -278,7 +300,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to download file from Azure Blob Storage ${path}`,
err,
);
- throw Error("Failed to download file from Azure Blob Storage");
+ handleStorageError(err, "download file from Azure Blob Storage");
}
}
@@ -303,7 +325,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to delete files from Azure Blob Storage ${paths}`,
err,
);
- throw Error("Failed to delete files from Azure Blob Storage");
+ handleStorageError(err, "delete files from Azure Blob Storage");
}
}
@@ -329,7 +351,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to list files from Azure Blob Storage ${prefix}`,
err,
);
- throw Error("Failed to list files from Azure Blob Storage");
+ handleStorageError(err, "list files from Azure Blob Storage");
}
}
@@ -361,7 +383,7 @@ class AzureBlobStorageService implements StorageService {
`Failed to generate presigned URL for Azure Blob Storage ${fileName}`,
err,
);
- throw Error("Failed to generate presigned URL for Azure Blob Storage");
+ handleStorageError(err, "generate presigned URL for Azure Blob Storage");
}
}
@@ -394,8 +416,9 @@ class AzureBlobStorageService implements StorageService {
`Failed to generate presigned upload URL for Azure Blob Storage ${path}`,
err,
);
- throw Error(
- "Failed to generate presigned upload URL for Azure Blob Storage",
+ handleStorageError(
+ err,
+ "generate presigned upload URL for Azure Blob Storage",
);
}
}
@@ -493,7 +516,7 @@ class S3StorageService implements StorageService {
return;
} catch (err) {
logger.error(`Failed to upload file to ${fileName}`, err);
- throw new Error(`Failed to upload to S3: ${err}`);
+ handleStorageError(err, "upload file to S3");
}
}
@@ -511,7 +534,7 @@ class S3StorageService implements StorageService {
return { signedUrl };
} catch (err) {
logger.error(`Failed to upload file to ${fileName}`, err);
- throw new Error(`Failed to upload to S3 or generate signed URL: ${err}`);
+ handleStorageError(err, "upload file to S3 or generate signed URL");
}
}
@@ -529,7 +552,7 @@ class S3StorageService implements StorageService {
await this.client.send(putCommand);
} catch (err) {
logger.error(`Failed to upload JSON to S3 ${path}`, err);
- throw Error("Failed to upload JSON to S3");
+ handleStorageError(err, "upload JSON to S3");
}
}
@@ -544,7 +567,7 @@ class S3StorageService implements StorageService {
return (await response.Body?.transformToString()) ?? "";
} catch (err) {
logger.error(`Failed to download file from S3 ${path}`, err);
- throw Error("Failed to download file from S3");
+ handleStorageError(err, "download file from S3");
}
}
@@ -568,7 +591,7 @@ class S3StorageService implements StorageService {
);
} catch (err) {
logger.error(`Failed to list files from S3 ${prefix}`, err);
- throw Error("Failed to list files from S3");
+ handleStorageError(err, "list files from S3");
}
}
@@ -591,7 +614,7 @@ class S3StorageService implements StorageService {
);
} catch (err) {
logger.error(`Failed to generate presigned URL for ${fileName}`, err);
- throw Error("Failed to generate signed URL");
+ handleStorageError(err, "generate signed URL");
}
}
@@ -633,7 +656,7 @@ class S3StorageService implements StorageService {
error: err,
files: paths,
});
- throw new Error("Failed to delete files from S3");
+ handleStorageError(err, "delete files from S3");
}
}
@@ -734,7 +757,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to upload file to Google Cloud Storage ${fileName}`,
err,
);
- throw new Error("Failed to upload to Google Cloud Storage");
+ handleStorageError(err, "upload file to Google Cloud Storage");
}
}
@@ -753,7 +776,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to upload file to Google Cloud Storage ${fileName}`,
err,
);
- throw new Error("Failed to upload to Google Cloud Storage");
+ handleStorageError(err, "upload file to Google Cloud Storage");
}
}
@@ -774,7 +797,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to upload JSON to Google Cloud Storage ${path}`,
err,
);
- throw Error("Failed to upload JSON to Google Cloud Storage");
+ handleStorageError(err, "upload JSON to Google Cloud Storage");
}
}
@@ -789,7 +812,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to download file from Google Cloud Storage ${path}`,
err,
);
- throw Error("Failed to download file from Google Cloud Storage");
+ handleStorageError(err, "download file from Google Cloud Storage");
}
}
@@ -808,7 +831,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to list files from Google Cloud Storage ${prefix}`,
err,
);
- throw Error("Failed to list files from Google Cloud Storage");
+ handleStorageError(err, "list files from Google Cloud Storage");
}
}
@@ -837,7 +860,7 @@ class GoogleCloudStorageService implements StorageService {
`Failed to generate signed URL for Google Cloud Storage ${fileName}`,
err,
);
- throw Error("Failed to generate signed URL for Google Cloud Storage");
+ handleStorageError(err, "generate signed URL for Google Cloud Storage");
}
}
@@ -870,8 +893,9 @@ class GoogleCloudStorageService implements StorageService {
`Failed to generate signed upload URL for Google Cloud Storage ${path}`,
err,
);
- throw Error(
- "Failed to generate signed upload URL for Google Cloud Storage",
+ handleStorageError(
+ err,
+ "generate signed upload URL for Google Cloud Storage",
);
}
}
@@ -886,7 +910,7 @@ class GoogleCloudStorageService implements StorageService {
);
} catch (err) {
logger.error(`Failed to delete files from Google Cloud Storage`, err);
- throw Error("Failed to delete files from Google Cloud Storage");
+ handleStorageError(err, "delete files from Google Cloud Storage");
}
}
}
diff --git a/web/src/__tests__/withMiddlewares.servertest.ts b/web/src/__tests__/withMiddlewares.servertest.ts
index f101b6ca15..7a7e1fefa1 100644
--- a/web/src/__tests__/withMiddlewares.servertest.ts
+++ b/web/src/__tests__/withMiddlewares.servertest.ts
@@ -5,6 +5,7 @@ import {
BaseError,
LangfuseNotFoundError,
UnauthorizedError,
+ ServiceUnavailableError,
} from "@langfuse/shared";
import {
ClickHouseResourceError,
@@ -282,6 +283,39 @@ describe("withMiddlewares error handling", () => {
});
});
+ describe("ServiceUnavailableError handling", () => {
+ it("should handle ServiceUnavailableError with 503 status", async () => {
+ const error = new ServiceUnavailableError(
+ "Storage service temporarily unavailable due to network issues",
+ );
+
+ const handler = withMiddlewares({
+ POST: async () => {
+ throw error;
+ },
+ });
+
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "POST",
+ headers: {
+ "x-langfuse-public-key": "test-key",
+ },
+ });
+
+ await handler(req, res);
+
+ expect(res._getStatusCode()).toBe(503);
+ const jsonData = JSON.parse(res._getData());
+ expect(jsonData).toMatchObject({
+ message:
+ "Storage service temporarily unavailable due to network issues",
+ error: "ServiceUnavailableError",
+ });
+ // Should trace 5xx errors
+ expect(traceException).toHaveBeenCalledWith(error);
+ });
+ });
+
describe("Generic error handling", () => {
it("should handle generic Error instances with 500 status", async () => {
const error = new Error("Something went wrong");