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>
18 lines
566 B
TypeScript
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);
|
|
}
|
|
}
|