feat(core): add getErrorMessage helper for narrowing unknown errors

Designed to replace the \`catch (err: any) { ... err.message ... }\` pattern
across the repo. Keeps the catch binding typed as \`unknown\` (TS default)
while still producing a readable message.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gsxdsm
2026-04-23 18:30:55 -07:00
parent 651c5678d2
commit d1bd02b2c9
2 changed files with 20 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
/**
* Narrow an unknown caught error into a string message.
*
* Designed to replace the `catch (err: any) { ... err.message ... }` pattern:
* prefer `catch (err) { toast(getErrorMessage(err)) }` — keeps the binding
* typed as `unknown` (TS default with useUnknownInCatchVariables) while
* still producing a readable message.
*/
export function getErrorMessage(err: unknown): string {
if (err instanceof Error) return err.message;
if (typeof err === "string") return err;
try {
return JSON.stringify(err);
} catch {
return String(err);
}
}

View File

@@ -596,3 +596,6 @@ export type {
} from "./chat-types.js";
export { ChatStore } from "./chat-store.js";
export type { ChatStoreEvents } from "./chat-store.js";
// ── Error helpers ─────────────────────────────────────────
export { getErrorMessage } from "./error-message.js";