Project Files
src / errors / error-message.ts
/**
* Rendering of an arbitrary thrown value into a human-readable string. Lives outside
* `tool-error.ts` so the latter stays narrowly scoped to user-facing tool-error formatting.
*/
/**
* Extract a human-readable message from an arbitrary thrown value.
*
* @param error - Thrown value to stringify.
* @returns The error message when the value is an `Error`, otherwise the stringified value.
*/
export function errorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message
}
// Avoid exposing [object Object] for plain objects; attempt JSON serialization as a fallback.
try {
return JSON.stringify(error)
} catch {
return String(error)
}
}