Skip to content

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

chore: propagate tags onto 422 errors (#13707)

Valery Meleshkina1c43faf5006
diff --git a/packages/shared/src/server/repositories/clickhouse.ts b/packages/shared/src/server/repositories/clickhouse.ts
index fa985be78d..fdcab1b5bf 100644
--- a/packages/shared/src/server/repositories/clickhouse.ts
+++ b/packages/shared/src/server/repositories/clickhouse.ts
@@ -49,18 +49,27 @@ export class ClickHouseResourceError extends Error {
static ERROR_ADVICE_MESSAGE = RESOURCE_LIMIT_ERROR_MESSAGE;
public readonly errorType: ErrorType;
+ public readonly tags?: Record<string, string>;
- constructor(errType: ErrorType, originalError: Error) {
+ constructor(
+ errType: ErrorType,
+ originalError: Error,
+ tags?: Record<string, string>,
+ ) {
super(originalError.message, { cause: originalError });
this.name = "ClickHouseResourceError";
this.errorType = errType;
+ this.tags = tags;
// Preserve the original stack trace if available
if (originalError.stack) {
this.stack = originalError.stack;
}
}
- static wrapIfResourceError(originalError: Error): Error {
+ static wrapIfResourceError(
+ originalError: Error,
+ tags?: Record<string, string>,
+ ): Error {
const errorMessage = originalError.message || "";
for (const [type, config] of Object.entries(ERROR_TYPE_CONFIG) as Array<
@@ -74,7 +83,7 @@ export class ClickHouseResourceError extends Error {
);
if (hasDiscriminator) {
- return new ClickHouseResourceError(type, originalError);
+ return new ClickHouseResourceError(type, originalError, tags);
}
}
@@ -239,7 +248,10 @@ export async function* queryClickhouseStream<T>(
sendClickhouseQuery({ ...opts, format: "JSONEachRow", span }),
)
.catch((error) => {
- throw ClickHouseResourceError.wrapIfResourceError(error as Error);
+ throw ClickHouseResourceError.wrapIfResourceError(
+ error as Error,
+ opts.tags,
+ );
});
for await (const rows of res.stream<T>()) {
@@ -249,7 +261,10 @@ export async function* queryClickhouseStream<T>(
}
} catch (error) {
if (error instanceof ClickHouseResourceError) throw error;
- throw ClickHouseResourceError.wrapIfResourceError(error as Error);
+ throw ClickHouseResourceError.wrapIfResourceError(
+ error as Error,
+ opts.tags,
+ );
} finally {
span.end();
}
@@ -441,7 +456,10 @@ export async function queryClickhouse<T>(
maxDelay: 100,
},
).catch((error) => {
- throw ClickHouseResourceError.wrapIfResourceError(error as Error);
+ throw ClickHouseResourceError.wrapIfResourceError(
+ error as Error,
+ opts.tags,
+ );
});
},
);
@@ -474,7 +492,10 @@ export async function* queryClickhouseWithProgress<T>(
}),
)
.catch((error) => {
- throw ClickHouseResourceError.wrapIfResourceError(error as Error);
+ throw ClickHouseResourceError.wrapIfResourceError(
+ error as Error,
+ opts.tags,
+ );
});
for await (const rows of res.stream()) {
@@ -484,7 +505,10 @@ export async function* queryClickhouseWithProgress<T>(
}
} catch (error) {
if (error instanceof ClickHouseResourceError) throw error;
- throw ClickHouseResourceError.wrapIfResourceError(error as Error);
+ throw ClickHouseResourceError.wrapIfResourceError(
+ error as Error,
+ opts.tags,
+ );
} finally {
span.end();
}
diff --git a/web/src/__tests__/server/repositories/clickhouse-resource-errors.servertest.ts b/web/src/__tests__/server/repositories/clickhouse-resource-errors.servertest.ts
index 565eb91498..7b8025bb32 100644
--- a/web/src/__tests__/server/repositories/clickhouse-resource-errors.servertest.ts
+++ b/web/src/__tests__/server/repositories/clickhouse-resource-errors.servertest.ts
@@ -16,6 +16,7 @@ describe("ClickHouse Resource Error Handling", () => {
res = await queryClickhouse<any>({
query: `SELECT throwIf(number >= 2, 'memory limit exceeded: would use 10.23 GiB') AS v FROM system.numbers LIMIT 2000`,
clickhouseSettings: { max_block_size: `${blockSize}` },
+ tags: { test: "marker" },
});
fail(
"Should have thrown an error, observed instead " +
@@ -24,6 +25,7 @@ describe("ClickHouse Resource Error Handling", () => {
} catch (error: any) {
expect(error).toBeInstanceOf(ClickHouseResourceError);
expect(error.errorType).toBe("MEMORY_LIMIT");
+ expect(error.tags?.test).toBe("marker");
}
});
});
diff --git a/web/src/__tests__/server/withMiddlewares.servertest.ts b/web/src/__tests__/server/withMiddlewares.servertest.ts
index aa89da0767..db57ced9a4 100644
--- a/web/src/__tests__/server/withMiddlewares.servertest.ts
+++ b/web/src/__tests__/server/withMiddlewares.servertest.ts
@@ -215,6 +215,39 @@ describe("withMiddlewares error handling", () => {
expect(jsonData["error"]).toBe("Request timed out");
});
+ it("should include tags from the error in the warn log", async () => {
+ const originalError = new Error("Memory limit exceeded");
+ const resourceError = new ClickHouseResourceError(
+ "MEMORY_LIMIT",
+ originalError,
+ { type: "events", kind: "publicApiRows" },
+ );
+
+ const handler = withMiddlewares({
+ GET: async () => {
+ throw resourceError;
+ },
+ });
+
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "GET",
+ headers: {
+ "x-langfuse-public-key": "test-key",
+ },
+ });
+
+ await handler(req, res);
+
+ expect(res._getStatusCode()).toBe(422);
+ expect(logger.warn).toHaveBeenCalledWith(
+ "ClickHouse resource limit exceeded",
+ expect.objectContaining({
+ errorType: "MEMORY_LIMIT",
+ tags: { type: "events", kind: "publicApiRows" },
+ }),
+ );
+ });
+
it("should handle ClickHouseResourceError with custom advice", async () => {
const originalError = new Error("Timeout exceeded");
const resourceError = new ClickHouseResourceError(
diff --git a/web/src/features/public-api/server/withMiddlewares.ts b/web/src/features/public-api/server/withMiddlewares.ts
index 0d41927004..0bbd9fd404 100644
--- a/web/src/features/public-api/server/withMiddlewares.ts
+++ b/web/src/features/public-api/server/withMiddlewares.ts
@@ -148,6 +148,7 @@ export function withMiddlewares(
errorType: resourceError.errorType,
message: resourceError.message,
suggestion: errorMessage,
+ tags: resourceError.tags,
});
return res.status(422).json({
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index e1928b508b..8e475078cd 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -170,6 +170,11 @@ const withErrorHandling = t.middleware(async ({ ctx, next }) => {
if (res.error.cause instanceof ClickHouseResourceError) {
// Surface ClickHouse errors using an advice message
// which is supposed to provide a bit of guidance to the user.
+ logger.warn("ClickHouse resource limit exceeded", {
+ errorType: res.error.cause.errorType,
+ message: res.error.cause.message,
+ tags: res.error.cause.tags,
+ });
logErrorByCode("UNPROCESSABLE_CONTENT", res.error);
res.error = new TRPCError({
code: "UNPROCESSABLE_CONTENT",