Open menu dropdown
getErrorMessage
Extract a human-readable message from anything thrown — `Error`, `string`, `{ message }` objects, or arbitrary values.
Especially useful inside catch blocks where the caught value is typed unknown.
Usage
ts
import { getErrorMessage } from '@maz-ui/utils'
try {
await doSomething()
}
catch (error) {
console.error(getErrorMessage(error))
}API
ts
function getErrorMessage(error: unknown): stringResolution rules
The helper inspects error in this order:
error instanceof Error→ returnserror.message.typeof error === 'string'→ returnserroras-is.erroris a plain object with amessageproperty → returnsString(error.message).erroris otherwise truthy → returnsString(error).erroris falsy → returns'An unexpected error occurred'.