move to thunk and add tests
This commit is contained in:
parent
0df33d4727
commit
23d095bbe7
4 changed files with 634 additions and 83 deletions
|
|
@ -6,8 +6,7 @@ import {
|
|||
KeyIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { DISCORD_LINK, GITHUB_LINK } from "core/util/constants";
|
||||
import posthog from "posthog-js";
|
||||
import { useContext, useEffect, useMemo } from "react";
|
||||
import { useContext, useMemo } from "react";
|
||||
import { GhostButton, SecondaryButton } from "../../components";
|
||||
import { useMainEditor } from "../../components/mainInput/TipTapEditor";
|
||||
import { DiscordIcon } from "../../components/svg/DiscordIcon";
|
||||
|
|
@ -21,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);
|
||||
|
|
@ -50,10 +35,14 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => {
|
|||
const { session, refreshProfiles } = useAuth();
|
||||
const { mainEditor } = useMainEditor();
|
||||
|
||||
const parsedError = useMemo<string>(
|
||||
() => parseErrorMessage((error as any)?.message || ""),
|
||||
[error],
|
||||
);
|
||||
const {
|
||||
parsedError,
|
||||
statusCode,
|
||||
message,
|
||||
modelTitle,
|
||||
providerName,
|
||||
apiKeyUrl,
|
||||
} = useMemo(() => analyzeError(error, selectedModel), [error, selectedModel]);
|
||||
|
||||
const handleRefreshProfiles = () => {
|
||||
void refreshProfiles();
|
||||
|
|
@ -67,70 +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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Capture error event with PostHog
|
||||
useEffect(() => {
|
||||
if (error) {
|
||||
const errorData = {
|
||||
error_type: statusCode ? `HTTP ${statusCode}` : "Unknown",
|
||||
error_message: parsedError,
|
||||
model_provider: providerName,
|
||||
model_title: modelTitle,
|
||||
};
|
||||
|
||||
posthog.capture("gui_stream_error", errorData);
|
||||
}
|
||||
}, [error, parsedError, statusCode, providerName, modelTitle]);
|
||||
|
||||
const checkKeysButton = apiKeyUrl ? (
|
||||
<GhostButton
|
||||
className="flex items-center"
|
||||
onClick={() => {
|
||||
ideMessenger.post("openUrl", apiKeyUrl!);
|
||||
ideMessenger.post("controlPlane/openUrl", {
|
||||
path: apiKeyUrl,
|
||||
orgSlug: undefined,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<KeyIcon className="mr-1.5 h-3.5 w-3.5" />
|
||||
|
|
@ -363,7 +296,10 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => {
|
|||
<GhostButton
|
||||
className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
|
||||
onClick={() => {
|
||||
ideMessenger.post("openUrl", GITHUB_LINK);
|
||||
ideMessenger.post("controlPlane/openUrl", {
|
||||
path: GITHUB_LINK,
|
||||
orgSlug: undefined,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<GithubIcon className="h-5 w-5" />
|
||||
|
|
@ -372,7 +308,10 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => {
|
|||
<GhostButton
|
||||
className="flex flex-row items-center gap-2 rounded px-3 py-1.5"
|
||||
onClick={() => {
|
||||
ideMessenger.post("openUrl", DISCORD_LINK);
|
||||
ideMessenger.post("controlPlane/openUrl", {
|
||||
path: DISCORD_LINK,
|
||||
orgSlug: undefined,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<DiscordIcon className="h-5 w-5" />
|
||||
|
|
|
|||
|
|
@ -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 { setInactive } from "../slices/sessionSlice";
|
||||
import { setDialogMessage, setShowDialog } from "../slices/uiSlice";
|
||||
import { ThunkApiType } from "../store";
|
||||
|
|
@ -11,6 +14,7 @@ export const streamThunkWrapper = createAsyncThunk<
|
|||
() => Promise<void>,
|
||||
ThunkApiType
|
||||
>("chat/streamWrapper", async (runStream, { dispatch, extra, getState }) => {
|
||||
const initialState = getState();
|
||||
try {
|
||||
await runStream();
|
||||
const state = getState();
|
||||
|
|
@ -27,5 +31,23 @@ export const streamThunkWrapper = createAsyncThunk<
|
|||
dispatch(cancelStream());
|
||||
dispatch(setDialogMessage(<StreamErrorDialog error={e} />));
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
508
gui/src/util/errorAnalysis.test.ts
Normal file
508
gui/src/util/errorAnalysis.test.ts
Normal file
|
|
@ -0,0 +1,508 @@
|
|||
import { analyzeError } from "./errorAnalysis";
|
||||
import { providers } from "../pages/AddNewModel/configs/providers";
|
||||
|
||||
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.parsedError).toBe('{"message": "Invalid API key", "type": "invalid_request_error"}');
|
||||
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");
|
||||
expect(result.parsedError).toBe('{"type": "rate_limit_error", "message": "Rate limit exceeded"}');
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
82
gui/src/util/errorAnalysis.ts
Normal file
82
gui/src/util/errorAnalysis.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
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.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;
|
||||
}
|
||||
}
|
||||
|
||||
export function analyzeError(error: unknown, selectedModel: any): ErrorAnalysis {
|
||||
const parsedError = parseErrorMessage((error as any)?.message || "");
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
parsedError,
|
||||
statusCode,
|
||||
message,
|
||||
modelTitle,
|
||||
providerName,
|
||||
apiKeyUrl,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue