For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/conversion/toError.md.

toError

Coerces an unknown thrown value into a proper Error instance.

Handles the common cases where libraries throw non-Error values (e.g. plain API response bodies, arrays, Maps) that would otherwise serialize as [object Object] in error messages.

try {
  await riskyCall()
} catch (thrown) {
  const error = toError(thrown)
  console.error(error.message)
}

Usage

toError(thrown: unknown)

Coerces an unknown thrown value into a proper Error instance.

Handles the common cases where libraries throw non-Error values (e.g. plain API response bodies, arrays, Maps) that would otherwise serialize as [object Object] in error messages.

import { toError } from 'massaman'
// or:  import { toError } from 'massaman/conversion'

try {
  await riskyCall()
} catch (thrown) {
  const error = toError(thrown)
  console.error(error.message)
}

Parameters

thrownunknownrequired

the caught value to normalize.

Returns

An Error with a meaningful .message. If thrown is already an Error, it is returned as-is. The original value is preserved as .cause for debugging.

Error