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:
17
packages/core/src/error-message.ts
Normal file
17
packages/core/src/error-message.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user