fix: expose assistant provider errors

This commit is contained in:
Thomas Camlong committed 2026-08-14 22:37:28 +02:00
1 parent 3c533f2d96
commit 28e58a5a5c
2 files changed
+24 -4

No files matched your search

@@ -54,4 +54,20 @@ describe("assistant stream errors", () => {
])("prefers actionable status %s over a generic input-stream phrase", (statusCode, expected) => {
expect(getAssistantStreamErrorMessage({ statusCode, responseBody: "Error in input stream" })).toBe(expected);
});
test("returns the provider error instead of replacing it with a generic message", () => {
expect(getAssistantStreamErrorMessage(new Error("Provider connection failed: certificate expired"))).toBe(
"Provider connection failed: certificate expired",
);
});
test("returns nested provider error details", () => {
expect(
getAssistantStreamErrorMessage({
error: {
message: "No endpoints found for the selected model",
},
}),
).toBe("No endpoints found for the selected model");
});
});
@@ -14,15 +14,19 @@ const getStatusCode = (error: unknown): number | undefined => {
return getStatusCode(record.cause) ?? getStatusCode(record.response);
};
const getErrorMessage = (error: unknown): string => {
const getErrorMessage = (error: unknown, seen = new Set<object>()): string => {
if (error instanceof Error) {
return `${error.message} ${getErrorMessage(error.cause)}`.trim();
if (seen.has(error)) return "";
seen.add(error);
return `${error.message} ${getErrorMessage(error.cause, seen)}`.trim();
}
const record = asRecord(error);
if (!record) return typeof error === "string" ? error : "";
if (seen.has(record)) return "";
seen.add(record);
const responseBody = typeof record.responseBody === "string" ? record.responseBody : "";
const message = typeof record.message === "string" ? record.message : "";
return `${message} ${responseBody} ${getErrorMessage(record.cause)}`.trim();
return `${message} ${responseBody} ${getErrorMessage(record.error, seen)} ${getErrorMessage(record.cause, seen)}`.trim();
};
export const getAssistantStreamErrorMessage = (error: unknown) => {
@@ -65,5 +69,5 @@ export const getAssistantStreamErrorMessage = (error: unknown) => {
return "The model provider interrupted the streamed response before the assistant could finish. Try again.";
}
return "The model endpoint stopped the response. Try again, or ask an administrator to verify its URL, model, and credentials.";
return message || "The model endpoint stopped without providing an error message.";
};