Files
fusion/packages/core/src/error-message.ts
gsxdsm e3b6965620 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>
2026-04-23 23:02:36 -07:00

18 lines
566 B
TypeScript

/**
* 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);
}
}