Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions gui/src/util/errorAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("errorAnalysis", () => {
expect(result.parsedError).toBe("");
});

it("should handle complex nested JSON", () => {
it("should handle complex nested JSON by returning the nested message string", () => {
const complexJson = {
error: {
code: 400,
Expand All @@ -141,7 +141,59 @@ describe("errorAnalysis", () => {
const error = new Error(`Header\n\n${JSON.stringify(complexJson)}`);
const result = analyzeError(error, null);

expect(result.parsedError).toBe(JSON.stringify(complexJson.error));
expect(result.parsedError).toBe("Bad Request");
});

it("should extract nested message from structured error object", () => {
const errorJson = {
error: {
message: "Quota exceeded",
status: "RESOURCE_EXHAUSTED",
},
};
const error = new Error(`Header\n\n${JSON.stringify(errorJson)}`);
const result = analyzeError(error, null);

expect(result.parsedError).toBe("Quota exceeded");
});

it("should preserve fallback behavior for nested error object without message", () => {
const errorJson = {
error: {
status: "RESOURCE_EXHAUSTED",
},
};
const error = new Error(`Header\n\n${JSON.stringify(errorJson)}`);
const result = analyzeError(error, null);

expect(result.parsedError).toBe('{"status":"RESOURCE_EXHAUSTED"}');
});

it("should handle primitive string error with no regression", () => {
const errorJson = {
error: "Invalid API key",
};
const error = new Error(`Header\n\n${JSON.stringify(errorJson)}`);
const result = analyzeError(error, null);

expect(result.parsedError).toBe('"Invalid API key"');
});

it("should handle top-level message with no regression", () => {
const errorJson = {
message: "Simple message",
};
const error = new Error(`Header\n\n${JSON.stringify(errorJson)}`);
const result = analyzeError(error, null);

expect(result.parsedError).toBe('"Simple message"');
});

it("should handle malformed JSON with no regression", () => {
const error = new Error("Header\n\n{invalid json}");
const result = analyzeError(error, null);

expect(result.parsedError).toBe("{invalid json}");
});
});

Expand Down Expand Up @@ -386,7 +438,7 @@ describe("errorAnalysis", () => {
};
const result = analyzeError(error, selectedModel);

expect(result.parsedError).toBe(JSON.stringify(errorObj.error));
expect(result.parsedError).toBe("Invalid request");
expect(result.statusCode).toBe(400);
});

Expand Down
7 changes: 7 additions & 0 deletions gui/src/util/errorAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ function parseErrorMessage(fullErrMsg: string): string {
try {
const parsed = JSON.parse(msg);
if (parsed.error !== undefined && parsed.error !== null) {
if (
typeof parsed.error === "object" &&
parsed.error !== null &&
typeof (parsed.error as any).message === "string"
) {
return (parsed.error as any).message;
}
return JSON.stringify(parsed.error);
}
if (parsed.message !== undefined && parsed.message !== null) {
Expand Down
Loading