AstrBot-Desktop/src/lib/errorMessage.ts

53 行
1.9 KiB
TypeScript

/**
* 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.
*/
fix(tauri): grant the window controls the permissions they call The minimize, maximize and close buttons did nothing. They were wired correctly; the ACL rejected them. core:default resolves to core:window:default, which grants only read-only queries — is-maximized is in it, which is why the maximize icon tracked state correctly, but minimize, toggle-maximize, close and start-dragging are not. Both windows are decorations: false, so with close denied the main window could not be closed at all except through the tray. Add two capabilities rather than widening the shared one: the main window gets the four controls it draws, and the pet window gets dragging alone, since it is moved by dragging its body and must not be able to close or resize itself. Every one of these calls was a bare void expression, so the rejection was swallowed and looked identical to an unwired button. Route them through a helper that reports the failure in the shell error banner. This is the second time a missing capability has surfaced as a silent no-op — the pet window previously had no entry at all. Add tests that read the shipped capability files and assert each permission the UI calls is granted to the window that calls it. Also translate the TypeError the Tauri shim throws outside the application into a sentence a user can act on. The dev server is reachable at 127.0.0.1:1420, so opening it in a browser and hitting save produced "Cannot read properties of undefined (reading 'invoke')". Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 01:19:42 +08:00
/**
* The Tauri API shim reads `window.__TAURI_INTERNALS__.invoke`, so calling any
* command from a plain browser tab throws a `TypeError` about reading 'invoke'
* rather than anything a user could act on. The dev server is reachable at
* 127.0.0.1:1420, so this is easy to hit by accident.
*/
const IPC_UNAVAILABLE_PATTERN = /reading ['"]?invoke['"]?|__TAURI_INTERNALS__/;
export const IPC_UNAVAILABLE_MESSAGE =
"此页面不在 AstrBot Desktop 应用窗口内运行,无法使用本地功能。请在应用中操作。";
export function errorMessage(reason: unknown): string {
if (reason instanceof Error) {
fix(tauri): grant the window controls the permissions they call The minimize, maximize and close buttons did nothing. They were wired correctly; the ACL rejected them. core:default resolves to core:window:default, which grants only read-only queries — is-maximized is in it, which is why the maximize icon tracked state correctly, but minimize, toggle-maximize, close and start-dragging are not. Both windows are decorations: false, so with close denied the main window could not be closed at all except through the tray. Add two capabilities rather than widening the shared one: the main window gets the four controls it draws, and the pet window gets dragging alone, since it is moved by dragging its body and must not be able to close or resize itself. Every one of these calls was a bare void expression, so the rejection was swallowed and looked identical to an unwired button. Route them through a helper that reports the failure in the shell error banner. This is the second time a missing capability has surfaced as a silent no-op — the pet window previously had no entry at all. Add tests that read the shipped capability files and assert each permission the UI calls is granted to the window that calls it. Also translate the TypeError the Tauri shim throws outside the application into a sentence a user can act on. The dev server is reachable at 127.0.0.1:1420, so opening it in a browser and hitting save produced "Cannot read properties of undefined (reading 'invoke')". Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 01:19:42 +08:00
if (IPC_UNAVAILABLE_PATTERN.test(reason.message)) {
return IPC_UNAVAILABLE_MESSAGE;
}
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
);
}