# toError
Source: https://docs.maelstrom-ai.co/0.2/reference/protocol/functions/toerror/
Documentation revision: 89a8f5e9d10cedcf0b30376d7846113f06f26120
SDK manifests in this build: @maelstrom-co/react@0.2.1, @maelstrom-co/client@0.2.1, @maelstrom-co/connectors@0.2.1, @maelstrom-co/protocol@0.2.1, @maelstrom-co/engine@0.2.1
These docs follow this source revision. Manifest versions do not prove release compatibility; verify the installed package exports before using an API.

> **toError**(`value`): `Error`

Defined in: packages/protocol/src/lib/to-error.ts:20

Coerce an unknown thrown value into an `Error` instance.

`try { ... } catch (e) { ... }` typically yields `e: unknown`. Most error- handling sites need an `Error` instance to attach as `cause`, log a stack, or propagate through APIs that require `Error`. This helper preserves the original `Error` if one was thrown, and otherwise wraps the stringified value in a new `Error` so the call site never has to branch.

## Parameters

### value

`unknown`

## Returns

`Error`

## Example

```typescript
try {
  await risky();
} catch (err) {
  logger.error('risky failed', toError(err));
}
```
