From e8d9dbc8f6cbb2555da24ff1a2c48cfae39e237e Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 26 Jul 2026 22:31:16 +0800 Subject: [PATCH] refactor(ui): share ipc error formatting Tauri rejects `invoke` with the deserialized Rust error value, which for `RuntimeCommandError { code, message }` is a plain object rather than an `Error`. Nine call sites used `reason instanceof Error ? reason.message : String(reason)`, so every send, reconnect, stop, and settings failure showed the user `[object Object]`. `ProfileManager` already had a correct implementation; this lifts it into `src/lib/errorMessage.ts` and routes the other call sites through it. The module also exports `isCancelledError`, which a later batch needs to tell a deliberate stop apart from a real failure. Refs P1-16 / F-08. Co-Authored-By: Claude Opus 5 --- src/App.tsx | 5 ++-- src/features/chat/ChatPanel.tsx | 15 +++++----- src/features/profiles/ProfileManager.tsx | 16 +--------- src/lib/errorMessage.ts | 38 ++++++++++++++++++++++++ src/pages/SettingsPages.tsx | 3 +- 5 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 src/lib/errorMessage.ts diff --git a/src/App.tsx b/src/App.tsx index 7e60089..671b962 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -10,6 +10,7 @@ import { useState, } from "react"; +import { errorMessage } from "./lib/errorMessage"; import { ChatPanel } from "./features/chat/ChatPanel"; import { StatusBubble } from "./features/chat/StatusBubble"; const ProfileManager = lazy(() => @@ -101,7 +102,7 @@ export function App() { setStatus(bootstrapStatusSchema.parse(result)); setError(null); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } }, []); @@ -121,7 +122,7 @@ export function App() { ? "open_pet_window" : "close_pet_window"; void invoke(command).catch((reason) => { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); }); }, [isBubbleView, isPetView, isQuickView, petDisplayMode]); diff --git a/src/features/chat/ChatPanel.tsx b/src/features/chat/ChatPanel.tsx index 820386f..41c5908 100644 --- a/src/features/chat/ChatPanel.tsx +++ b/src/features/chat/ChatPanel.tsx @@ -2,6 +2,7 @@ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; +import { errorMessage } from "../../lib/errorMessage"; import { MessageContent } from "./MessageContent"; import { @@ -375,7 +376,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { } setPendingAttachments((current) => current.concat(uploaded)); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } finally { setUploading(false); } @@ -421,7 +422,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { ); setError(null); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); setMessages((current) => current.map((item) => item.id === id ? { ...item, error: true } : item, @@ -452,7 +453,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { ); setError(null); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } }; @@ -499,7 +500,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { setStatus((current) => ({ ...current, [selectedSlot]: result })); setError(null); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } }; @@ -522,7 +523,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { setStatus((current) => ({ ...current, [selectedSlot]: result })); setError(null); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } }; @@ -533,7 +534,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { }); await refresh(); } catch (reason) { - setError(reason instanceof Error ? reason.message : String(reason)); + setError(errorMessage(reason)); } }; @@ -697,7 +698,7 @@ export function ChatPanel({ fixedSlot }: ChatPanelProps) { .then(() => void refresh()) .catch((reason) => setError( - reason instanceof Error ? reason.message : String(reason), + errorMessage(reason), ), ); }} diff --git a/src/features/profiles/ProfileManager.tsx b/src/features/profiles/ProfileManager.tsx index 024bf9a..8196878 100644 --- a/src/features/profiles/ProfileManager.tsx +++ b/src/features/profiles/ProfileManager.tsx @@ -21,6 +21,7 @@ import { type UpdateConnectionProfileRequest, } from "../../types/profile"; import { connectionRuntimeStatusSchema } from "../chat/types"; +import { errorMessage } from "../../lib/errorMessage"; import { CredentialEnrollmentCard } from "./CredentialEnrollmentCard"; interface ProfileFormState { @@ -685,18 +686,3 @@ function slotLabel(slot: ActiveSlotKind) { function connectionStateLabel(state: ConnectionSlot["state"]) { return state === "disconnected" ? "未连接" : state; } - -function errorMessage(reason: unknown) { - if (reason instanceof Error) { - return reason.message; - } - if ( - typeof reason === "object" && - reason !== null && - "message" in reason && - typeof reason.message === "string" - ) { - return reason.message; - } - return String(reason); -} diff --git a/src/lib/errorMessage.ts b/src/lib/errorMessage.ts new file mode 100644 index 0000000..4a150e6 --- /dev/null +++ b/src/lib/errorMessage.ts @@ -0,0 +1,38 @@ +/** + * Normalizes a rejection reason into a human-readable string. + * + * Tauri `invoke` rejects with the deserialized Rust error value, which is a + * plain object (for example `RuntimeCommandError { code, message }`) rather + * than an `Error` instance. Passing such a value to `String()` yields + * "[object Object]", so every IPC failure has to go through this helper. + */ +export function errorMessage(reason: unknown): string { + if (reason instanceof Error) { + return reason.message; + } + if (typeof reason === "string") { + return reason; + } + if (typeof reason === "object" && reason !== null) { + const candidate = reason as { message?: unknown; code?: unknown }; + if (typeof candidate.message === "string" && candidate.message.length > 0) { + return candidate.message; + } + if (typeof candidate.code === "string" && candidate.code.length > 0) { + return candidate.code; + } + } + return String(reason); +} + +/** Rust returns this code when a request was cancelled on purpose. */ +export const CANCELLED_ERROR_CODE = "cancelled"; + +/** True when a rejection reason is a deliberate cancellation rather than a failure. */ +export function isCancelledError(reason: unknown): boolean { + return ( + typeof reason === "object" && + reason !== null && + (reason as { code?: unknown }).code === CANCELLED_ERROR_CODE + ); +} diff --git a/src/pages/SettingsPages.tsx b/src/pages/SettingsPages.tsx index f187afd..914e949 100644 --- a/src/pages/SettingsPages.tsx +++ b/src/pages/SettingsPages.tsx @@ -1,6 +1,7 @@ import { invoke } from "@tauri-apps/api/core"; import { useState } from "react"; +import { errorMessage } from "../lib/errorMessage"; import type { BootstrapStatus } from "../types/bootstrap"; export type SettingsChatSlot = "companion" | "operations"; @@ -117,7 +118,7 @@ function DiagnosticsTools() { ); setResult(JSON.stringify(value, null, 2)); } catch (reason) { - setResult(reason instanceof Error ? reason.message : String(reason)); + setResult(errorMessage(reason)); } finally { setBusy(false); }