mirror of
https://github.com/ONLYOFFICE/desktop-sdk.git
synced 2026-02-10 18:15:05 +08:00
AIAgent: add handle errors
This commit is contained in:
@ -24,23 +24,35 @@ const ConfigDialog = ({ open, onClose }: ConfigDialogProps) => {
|
||||
const editorRef = React.useRef<HTMLDivElement>(null);
|
||||
const viewRef = React.useRef<EditorView | null>(null);
|
||||
const [value, setValue] = React.useState("");
|
||||
// const [error, setError] = React.useState("");
|
||||
const [error, setError] = React.useState("");
|
||||
const [isValidJson, setIsValidJson] = React.useState(true);
|
||||
|
||||
const validateJson = (jsonString: string) => {
|
||||
try {
|
||||
JSON.parse(jsonString);
|
||||
// setError("");
|
||||
setIsValidJson(true);
|
||||
return true;
|
||||
} catch {
|
||||
// setError(
|
||||
// `Invalid JSON: ${err instanceof Error ? err.message : "Unknown error"}`
|
||||
// );
|
||||
setIsValidJson(false);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
const validateJson = React.useCallback(
|
||||
(jsonString: string) => {
|
||||
try {
|
||||
const parsed = JSON.parse(jsonString);
|
||||
if (parsed.mcpServers) {
|
||||
setIsValidJson(true);
|
||||
setError("");
|
||||
return true;
|
||||
}
|
||||
|
||||
setIsValidJson(false);
|
||||
setError(t("ConfigurationError"));
|
||||
|
||||
return false;
|
||||
} catch (err) {
|
||||
setError(
|
||||
`Invalid JSON format\n ${
|
||||
err instanceof Error ? err.message : "Unknown error"
|
||||
}`
|
||||
);
|
||||
setIsValidJson(false);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[t]
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!open) return;
|
||||
@ -109,7 +121,7 @@ const ConfigDialog = ({ open, onClose }: ConfigDialogProps) => {
|
||||
viewRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [open, getConfig]);
|
||||
}, [open, t, validateJson, getConfig]);
|
||||
|
||||
const onSubmitAction = React.useCallback(() => {
|
||||
if (!isValidJson) return;
|
||||
@ -139,10 +151,10 @@ const ConfigDialog = ({ open, onClose }: ConfigDialogProps) => {
|
||||
<DialogContent
|
||||
header={t("EditConfiguration")}
|
||||
onClose={onClose}
|
||||
className="w-[564px] h-[400px]"
|
||||
className="w-[564px] min-h-[400px]"
|
||||
>
|
||||
<div className="flex flex-col gap-[8px] h-[280px] pt-[8px] pb-[16px]">
|
||||
<div className="flex flex-col gap-[8px] h-[256px] p-[12px] bg-[var(--servers-edit-config-json-background-color)] rounded-[12px]">
|
||||
<div className="flex flex-col gap-[8px] min-h-[280px] pt-[8px] pb-[16px]">
|
||||
<div className="flex flex-col gap-[8px] min-h-[256px] p-[12px] bg-[var(--servers-edit-config-json-background-color)] rounded-[12px]">
|
||||
<div className="flex flex-row justify-between">
|
||||
<p className="font-bold text-[14px] leading-[20px] text-[var(--servers-edit-config-json-header-color)]">
|
||||
{t("EnterYourJSONConfiguration")}
|
||||
@ -155,6 +167,11 @@ const ConfigDialog = ({ open, onClose }: ConfigDialogProps) => {
|
||||
ref={editorRef}
|
||||
className="border border-[var(--servers-edit-config-json-editor-border-color)] bg-[var(--servers-edit-config-json-editor-background-color)] rounded-[4px] overflow-hidden h-full max-h-full"
|
||||
/>
|
||||
{error ? (
|
||||
<p className="text-[var(--text-negative)] font-normal text-[14px] leading-[20px] whitespace-pre-line">
|
||||
{error}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-row items-center justify-end gap-[16px] h-[64px] border-t border-[var(--servers-edit-config-buttons-border-color)] mx-[-32px] px-[32px]">
|
||||
|
||||
Reference in New Issue
Block a user