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 <noreply@anthropic.com>
这个提交存在于:
YokiiroBW 2026-07-26 22:31:16 +08:00
父节点 051b9ced6a
当前提交 e8d9dbc8f6
共有 5 个文件被更改,包括 52 次插入 和 25 次删除

查看文件

@ -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]);

查看文件

@ -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),
),
);
}}

查看文件

@ -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);
}

38
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
);
}

查看文件

@ -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);
}