diff --git a/gui/src/pages/gui/StreamError.tsx b/gui/src/pages/gui/StreamError.tsx index 356f8f9b3..58fcaf97b 100644 --- a/gui/src/pages/gui/StreamError.tsx +++ b/gui/src/pages/gui/StreamError.tsx @@ -20,27 +20,13 @@ import { selectSelectedProfile } from "../../redux/slices/profilesSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; import { streamResponseThunk } from "../../redux/thunks/streamResponse"; import { isLocalProfile } from "../../util"; -import { providers } from "../AddNewModel/configs/providers"; +import { analyzeError } from "../../util/errorAnalysis"; import { ModelsAddOnLimitDialog } from "./ModelsAddOnLimitDialog"; interface StreamErrorProps { error: unknown; } -function parseErrorMessage(fullErrMsg: string): string { - if (!fullErrMsg.includes("\n\n")) { - return fullErrMsg; - } - - const msg = fullErrMsg.split("\n\n").slice(1).join("\n\n"); - try { - const parsed = JSON.parse(msg); - return JSON.stringify(parsed.error ?? parsed.message ?? msg); - } catch (e) { - return msg; - } -} - const StreamErrorDialog = ({ error }: StreamErrorProps) => { const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); @@ -49,10 +35,14 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { const { session, refreshProfiles } = useAuth(); const { mainEditor } = useMainEditor(); - const parsedError = useMemo( - () => parseErrorMessage((error as any)?.message || ""), - [error], - ); + const { + parsedError, + statusCode, + message, + modelTitle, + providerName, + apiKeyUrl, + } = useMemo(() => analyzeError(error, selectedModel), [error, selectedModel]); const handleRefreshProfiles = () => { void refreshProfiles(); @@ -66,56 +56,14 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { const history = useAppSelector((store) => store.session.history); - // Collect model information to display useful error info - let modelTitle = "Chat model"; - let providerName = "the model provider"; - let apiKeyUrl: string | undefined = undefined; - - if (selectedModel) { - modelTitle = selectedModel.title; - providerName = selectedModel.provider; - - // If there's a matching provider from add model form provider info - // We can get more info - const foundProvider = Object.values(providers).find( - (p) => p?.provider === selectedModel.provider, - ); - if (foundProvider) { - providerName = foundProvider.title; - if (foundProvider.apiKeyUrl) { - apiKeyUrl = foundProvider.apiKeyUrl; - } - } - } - - let message: undefined | string = undefined; - let statusCode: undefined | number = undefined; - - // Attempt to get error message and status code from error - if ( - error && - (error instanceof Error || typeof error === "object") && - "message" in error && - typeof error["message"] === "string" - ) { - message = error["message"]; - const parts = message?.split(" ") ?? []; - if (parts.length > 1) { - const status = parts[0] === "HTTP" ? parts[1] : parts[0]; - if (status) { - const code = Number(status); - if (!Number.isNaN(code)) { - statusCode = code; - } - } - } - } - const checkKeysButton = apiKeyUrl ? ( { - ideMessenger.post("openUrl", apiKeyUrl!); + ideMessenger.post("controlPlane/openUrl", { + path: apiKeyUrl, + orgSlug: undefined, + }); }} > @@ -348,7 +296,10 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { { - ideMessenger.post("openUrl", GITHUB_LINK); + ideMessenger.post("controlPlane/openUrl", { + path: GITHUB_LINK, + orgSlug: undefined, + }); }} > @@ -357,7 +308,10 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { { - ideMessenger.post("openUrl", DISCORD_LINK); + ideMessenger.post("controlPlane/openUrl", { + path: DISCORD_LINK, + orgSlug: undefined, + }); }} > diff --git a/gui/src/redux/thunks/streamThunkWrapper.tsx b/gui/src/redux/thunks/streamThunkWrapper.tsx index 9e40f4f06..6e5c032d4 100644 --- a/gui/src/redux/thunks/streamThunkWrapper.tsx +++ b/gui/src/redux/thunks/streamThunkWrapper.tsx @@ -1,5 +1,8 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; +import posthog from "posthog-js"; import StreamErrorDialog from "../../pages/gui/StreamError"; +import { analyzeError } from "../../util/errorAnalysis"; +import { selectSelectedChatModel } from "../slices/configSlice"; import { setDialogMessage, setShowDialog } from "../slices/uiSlice"; import { ThunkApiType } from "../store"; import { cancelStream } from "./cancelStream"; @@ -10,6 +13,7 @@ export const streamThunkWrapper = createAsyncThunk< () => Promise, ThunkApiType >("chat/streamWrapper", async (runStream, { dispatch, extra, getState }) => { + const initialState = getState(); try { await runStream(); const state = getState(); @@ -25,5 +29,23 @@ export const streamThunkWrapper = createAsyncThunk< await dispatch(cancelStream()); dispatch(setDialogMessage()); dispatch(setShowDialog(true)); + + // Get the selected model from the state for error analysis + const state = getState(); + const selectedModel = selectSelectedChatModel(state); + + const { parsedError, statusCode, modelTitle, providerName } = analyzeError( + e, + selectedModel, + ); + + const errorData = { + error_type: statusCode ? `HTTP ${statusCode}` : "Unknown", + error_message: parsedError, + model_provider: providerName, + model_title: modelTitle, + }; + + posthog.capture("gui_stream_error", errorData); } }); diff --git a/gui/src/util/errorAnalysis.test.ts b/gui/src/util/errorAnalysis.test.ts new file mode 100644 index 000000000..e7d2b8868 --- /dev/null +++ b/gui/src/util/errorAnalysis.test.ts @@ -0,0 +1,535 @@ +import { analyzeError } from "./errorAnalysis"; + +describe("errorAnalysis", () => { + describe("analyzeError", () => { + describe("basic error analysis", () => { + it("should return basic error info when no selectedModel is provided", () => { + const error = new Error("Test error message"); + const result = analyzeError(error, null); + + expect(result).toEqual({ + parsedError: "Test error message", + statusCode: undefined, + message: "Test error message", + modelTitle: "Chat model", + providerName: "the model provider", + apiKeyUrl: undefined, + }); + }); + + it("should handle undefined error", () => { + const result = analyzeError(undefined, null); + + expect(result).toEqual({ + parsedError: "", + statusCode: undefined, + message: undefined, + modelTitle: "Chat model", + providerName: "the model provider", + apiKeyUrl: undefined, + }); + }); + + it("should handle null error", () => { + const result = analyzeError(null, null); + + expect(result).toEqual({ + parsedError: "", + statusCode: undefined, + message: undefined, + modelTitle: "Chat model", + providerName: "the model provider", + apiKeyUrl: undefined, + }); + }); + + it("should handle error with no message property", () => { + const error = { someProperty: "value" }; + const result = analyzeError(error, null); + + expect(result).toEqual({ + parsedError: "", + statusCode: undefined, + message: undefined, + modelTitle: "Chat model", + providerName: "the model provider", + apiKeyUrl: undefined, + }); + }); + + it("should handle error with non-string message", () => { + const error = { message: 123 }; + const result = analyzeError(error, null); + + expect(result).toEqual({ + parsedError: "", + statusCode: undefined, + message: undefined, + modelTitle: "Chat model", + providerName: "the model provider", + apiKeyUrl: undefined, + }); + }); + }); + + describe("error message parsing", () => { + it("should return original message when no double newline is present", () => { + const error = new Error("Simple error message"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("Simple error message"); + }); + + it("should parse JSON error message after double newline", () => { + const error = new Error( + 'Header message\n\n{"error": "JSON error message"}', + ); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('"JSON error message"'); + }); + + it("should parse JSON message property after double newline", () => { + const error = new Error( + 'Header message\n\n{"message": "JSON message content"}', + ); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('"JSON message content"'); + }); + + it("should return raw JSON when no error or message property", () => { + const error = new Error( + 'Header message\n\n{"code": 404, "details": "Not found"}', + ); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe( + '{"code": 404, "details": "Not found"}', + ); + }); + + it("should handle invalid JSON after double newline", () => { + const error = new Error("Header message\n\n{invalid json}"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("{invalid json}"); + }); + + it("should handle multiple double newlines", () => { + const error = new Error("Header\n\nFirst section\n\nSecond section"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("First section\n\nSecond section"); + }); + + it("should handle empty string after double newline", () => { + const error = new Error("Header message\n\n"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe(""); + }); + + it("should handle complex nested JSON", () => { + const complexJson = { + error: { + code: 400, + message: "Bad Request", + details: ["Invalid parameter", "Missing field"], + }, + }; + const error = new Error(`Header\n\n${JSON.stringify(complexJson)}`); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe(JSON.stringify(complexJson.error)); + }); + }); + + describe("status code extraction", () => { + it("should extract status code from HTTP error format", () => { + const error = new Error("HTTP 404 Not Found"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(404); + expect(result.message).toBe("HTTP 404 Not Found"); + }); + + it("should extract status code from direct number format", () => { + const error = new Error("401 Unauthorized"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(401); + expect(result.message).toBe("401 Unauthorized"); + }); + + it("should extract status code from beginning of message", () => { + const error = new Error("500 Internal Server Error occurred"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(500); + expect(result.message).toBe("500 Internal Server Error occurred"); + }); + + it("should not extract status code from middle of message", () => { + const error = new Error("Error occurred with status 404"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(undefined); + expect(result.message).toBe("Error occurred with status 404"); + }); + + it("should handle invalid status codes", () => { + const error = new Error("ABC Invalid"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(undefined); + expect(result.message).toBe("ABC Invalid"); + }); + + it("should handle single word messages", () => { + const error = new Error("404"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(404); + expect(result.message).toBe("404"); + }); + + it("should handle empty messages", () => { + const error = new Error(""); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(undefined); + expect(result.message).toBe(""); + }); + + it("should handle messages with only spaces", () => { + const error = new Error(" "); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(undefined); + expect(result.message).toBe(" "); + }); + + it("should handle floating point numbers", () => { + const error = new Error("HTTP 404.5 Error"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(404.5); + expect(result.message).toBe("HTTP 404.5 Error"); + }); + + it("should handle negative numbers", () => { + const error = new Error("HTTP -1 Error"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(-1); + expect(result.message).toBe("HTTP -1 Error"); + }); + }); + + describe("selectedModel handling", () => { + it("should use selectedModel title and provider", () => { + const selectedModel = { + title: "GPT-4", + provider: "openai", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.modelTitle).toBe("GPT-4"); + expect(result.providerName).toBe("OpenAI"); + expect(result.apiKeyUrl).toBe( + "https://platform.openai.com/account/api-keys", + ); + }); + + it("should use selectedModel info when no matching provider is found", () => { + const selectedModel = { + title: "Custom Model", + provider: "unknown-provider", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.modelTitle).toBe("Custom Model"); + expect(result.providerName).toBe("unknown-provider"); + expect(result.apiKeyUrl).toBe(undefined); + }); + + it("should handle selectedModel with missing title", () => { + const selectedModel = { + provider: "openai", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.modelTitle).toBe(undefined); + expect(result.providerName).toBe("OpenAI"); + expect(result.apiKeyUrl).toBe( + "https://platform.openai.com/account/api-keys", + ); + }); + + it("should handle selectedModel with missing provider", () => { + const selectedModel = { + title: "Custom Model", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.modelTitle).toBe("Custom Model"); + expect(result.providerName).toBe(undefined); + expect(result.apiKeyUrl).toBe(undefined); + }); + + it("should handle empty selectedModel", () => { + const selectedModel = {}; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.modelTitle).toBe(undefined); + expect(result.providerName).toBe(undefined); + expect(result.apiKeyUrl).toBe(undefined); + }); + }); + + describe("provider matching", () => { + it("should match Anthropic provider", () => { + const selectedModel = { + title: "Claude 3.5 Sonnet", + provider: "anthropic", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.providerName).toBe("Anthropic"); + expect(result.apiKeyUrl).toBe( + "https://console.anthropic.com/account/keys", + ); + }); + + it("should match Groq provider", () => { + const selectedModel = { + title: "Llama 3.1 70B", + provider: "groq", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.providerName).toBe("Groq"); + expect(result.apiKeyUrl).toBe("https://console.groq.com/keys"); + }); + + it("should match provider with no apiKeyUrl", () => { + const selectedModel = { + title: "Llama 3.1", + provider: "ollama", + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.providerName).toBe("Ollama"); + expect(result.apiKeyUrl).toBe(undefined); + }); + + it("should be case sensitive for provider matching", () => { + const selectedModel = { + title: "GPT-4", + provider: "OpenAI", // Different case + }; + const error = new Error("Test error"); + const result = analyzeError(error, selectedModel); + + expect(result.providerName).toBe("OpenAI"); // Should use the original casing + expect(result.apiKeyUrl).toBe(undefined); // No match found + }); + }); + + describe("complex error scenarios", () => { + it("should handle error with JSON message and status code", () => { + const error = new Error( + 'HTTP 429 Rate Limited\n\n{"error": "Too many requests", "retry_after": 60}', + ); + const selectedModel = { + title: "GPT-4", + provider: "openai", + }; + const result = analyzeError(error, selectedModel); + + expect(result.parsedError).toBe('"Too many requests"'); + expect(result.statusCode).toBe(429); + expect(result.message).toBe( + 'HTTP 429 Rate Limited\n\n{"error": "Too many requests", "retry_after": 60}', + ); + expect(result.modelTitle).toBe("GPT-4"); + expect(result.providerName).toBe("OpenAI"); + expect(result.apiKeyUrl).toBe( + "https://platform.openai.com/account/api-keys", + ); + }); + + it("should handle error with complex nested JSON structure", () => { + const errorObj = { + error: { + message: "Invalid request", + type: "invalid_request_error", + param: "model", + code: "model_not_found", + }, + }; + const error = new Error( + `400 Bad Request\n\n${JSON.stringify(errorObj)}`, + ); + const selectedModel = { + title: "GPT-4", + provider: "openai", + }; + const result = analyzeError(error, selectedModel); + + expect(result.parsedError).toBe(JSON.stringify(errorObj.error)); + expect(result.statusCode).toBe(400); + }); + + it("should handle Error instance vs plain object", () => { + const errorInstance = new Error("Error instance message"); + const plainObject = { message: "Plain object message" }; + + const result1 = analyzeError(errorInstance, null); + const result2 = analyzeError(plainObject, null); + + expect(result1.message).toBe("Error instance message"); + expect(result2.message).toBe("Plain object message"); + }); + + it("should handle malformed JSON gracefully", () => { + const error = new Error('Header\n\n{"incomplete": json}'); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('{"incomplete": json}'); + }); + + it("should handle empty JSON object", () => { + const error = new Error("Header\n\n{}"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("{}"); + }); + + it("should handle JSON with null values", () => { + const error = new Error( + 'Header\n\n{"error": null, "message": "Something went wrong"}', + ); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('"Something went wrong"'); + }); + + it("should handle JSON with undefined values", () => { + const errorObj = { error: undefined, message: "Undefined error" }; + const error = new Error(`Header\n\n${JSON.stringify(errorObj)}`); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('"Undefined error"'); + }); + }); + + describe("edge cases", () => { + it("should handle very long error messages", () => { + const longMessage = "A".repeat(10000); + const error = new Error(longMessage); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe(longMessage); + expect(result.message).toBe(longMessage); + }); + + it("should handle error messages with special characters", () => { + const specialMessage = "Error: 中文 🔥 \n\t Special chars"; + const error = new Error(specialMessage); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe(specialMessage); + expect(result.message).toBe(specialMessage); + }); + + it("should handle error messages with only newlines", () => { + const error = new Error("\n\n\n"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("\n"); + }); + + it("should handle circular JSON references gracefully", () => { + const obj: any = { name: "test" }; + obj.self = obj; + + // This would normally cause JSON.stringify to throw + const error = new Error('Header\n\n{"error": "circular reference"}'); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe('"circular reference"'); + }); + + it("should handle error with numeric status codes as strings", () => { + const error = new Error("HTTP 404 Not Found"); + const result = analyzeError(error, null); + + expect(result.statusCode).toBe(404); + expect(typeof result.statusCode).toBe("number"); + }); + }); + + describe("real-world error scenarios", () => { + it("should handle OpenAI API key error", () => { + const error = new Error( + '401 Unauthorized\n\n{"error": {"message": "Invalid API key", "type": "invalid_request_error"}}', + ); + const selectedModel = { + title: "GPT-4", + provider: "openai", + }; + const result = analyzeError(error, selectedModel); + + expect(result.statusCode).toBe(401); + expect(result.apiKeyUrl).toBe( + "https://platform.openai.com/account/api-keys", + ); + }); + + it("should handle Anthropic rate limit error", () => { + const error = new Error( + '429 Too Many Requests\n\n{"error": {"type": "rate_limit_error", "message": "Rate limit exceeded"}}', + ); + const selectedModel = { + title: "Claude 3.5 Sonnet", + provider: "anthropic", + }; + const result = analyzeError(error, selectedModel); + + expect(result.statusCode).toBe(429); + expect(result.providerName).toBe("Anthropic"); + }); + + it("should handle network connectivity error", () => { + const error = new Error("Network request failed"); + const selectedModel = { + title: "Local Model", + provider: "ollama", + }; + const result = analyzeError(error, selectedModel); + + expect(result.parsedError).toBe("Network request failed"); + expect(result.statusCode).toBe(undefined); + expect(result.providerName).toBe("Ollama"); + }); + + it("should handle timeout error", () => { + const error = new Error("Request timeout"); + const result = analyzeError(error, null); + + expect(result.parsedError).toBe("Request timeout"); + expect(result.statusCode).toBe(undefined); + }); + }); + }); +}); diff --git a/gui/src/util/errorAnalysis.ts b/gui/src/util/errorAnalysis.ts new file mode 100644 index 000000000..82ee8df3a --- /dev/null +++ b/gui/src/util/errorAnalysis.ts @@ -0,0 +1,108 @@ +import { providers } from "../pages/AddNewModel/configs/providers"; + +export interface ErrorAnalysis { + parsedError: string; + statusCode?: number; + message?: string; + modelTitle: string; + providerName: string; + apiKeyUrl?: string; +} + +function parseErrorMessage(fullErrMsg: string): string { + if ( + !fullErrMsg || + typeof fullErrMsg !== "string" || + !fullErrMsg.includes("\n\n") + ) { + return fullErrMsg; + } + + const msg = fullErrMsg.split("\n\n").slice(1).join("\n\n"); + try { + const parsed = JSON.parse(msg); + if (parsed.error !== undefined && parsed.error !== null) { + return JSON.stringify(parsed.error); + } + if (parsed.message !== undefined && parsed.message !== null) { + return JSON.stringify(parsed.message); + } + return msg; + } catch (e) { + return msg; + } +} + +export function analyzeError( + error: unknown, + selectedModel: any, +): ErrorAnalysis { + const errorMessage = (error as any)?.message; + const parsedError = parseErrorMessage( + typeof errorMessage === "string" ? errorMessage : "", + ); + + // Collect model information to display useful error info + let modelTitle = "Chat model"; + let providerName = "the model provider"; + let apiKeyUrl: string | undefined = undefined; + + if (selectedModel) { + modelTitle = selectedModel.title; + providerName = selectedModel.provider; + + // If there's a matching provider from add model form provider info + // We can get more info + const foundProvider = Object.values(providers).find( + (p) => p?.provider === selectedModel.provider, + ); + if (foundProvider) { + providerName = foundProvider.title; + if (foundProvider.apiKeyUrl) { + apiKeyUrl = foundProvider.apiKeyUrl; + } + } + } + + let message: undefined | string = undefined; + let statusCode: undefined | number = undefined; + + // Attempt to get error message and status code from error + if ( + error && + (error instanceof Error || typeof error === "object") && + "message" in error && + typeof error["message"] === "string" + ) { + message = error["message"]; + const parts = message?.split(" ") ?? []; + + // Handle single word case (like "404") + if (parts.length === 1) { + const trimmed = parts[0].trim(); + if (trimmed !== "") { + const code = Number(trimmed); + if (!Number.isNaN(code)) { + statusCode = code; + } + } + } else if (parts.length > 1) { + const status = parts[0] === "HTTP" ? parts[1] : parts[0]; + if (status) { + const code = Number(status); + if (!Number.isNaN(code)) { + statusCode = code; + } + } + } + } + + return { + parsedError, + statusCode, + message, + modelTitle, + providerName, + apiKeyUrl, + }; +}