# Error handling

The React SDK uses `OpenfortError` and `OpenfortErrorType` for typed error handling. All SDK errors extend `OpenfortError` and include a `type` for programmatic handling.

## Import

```tsx
import { OpenfortError, OpenfortErrorType } from '@openfort/react'
```

## OpenfortError

```ts
class OpenfortError extends Error {
  type: OpenfortErrorType
  data: Record<string, unknown>

  constructor(message: string, type: OpenfortErrorType, data?: Record<string, unknown>)
}
```

* `type` — Error type for branching (e.g. `OpenfortErrorType.AUTHENTICATION_ERROR`)
* `data` — Extra context as key/value pairs for debugging

## Error types (OpenfortErrorType)

| Type | Category |
|------|----------|
| `AUTHENTICATION_ERROR` | Authentication failures |
| `WALLET_ERROR` | Wallet creation, recovery, signing |
| `CONFIGURATION_ERROR` | Missing or invalid config |
| `VALIDATION_ERROR` | Input validation failures |
| `UNEXPECTED_ERROR` | Unexpected or unknown errors |

## Basic usage

```tsx
import { useEthereumEmbeddedWallet } from '@openfort/react/ethereum'
import { OpenfortError, OpenfortErrorType } from '@openfort/react'

function CreateWallet() {
  const { create } = useEthereumEmbeddedWallet()

  const handleCreate = async () => {
    try {
      await create()
    } catch (error) {
      if (error instanceof OpenfortError) {
        switch (error.type) {
          case OpenfortErrorType.AUTHENTICATION_ERROR:
            // Redirect to login
            break
          case OpenfortErrorType.WALLET_ERROR:
            // Show wallet error
            break
          case OpenfortErrorType.CONFIGURATION_ERROR:
            // Show config error
            break
          default:
            console.error(error.message)
        }
      } else {
        throw error
      }
    }
  }

  return <button onClick={handleCreate}>Create wallet</button>
}
```

## OpenfortHookOptions

Many hooks accept `onSuccess`, `onError`, and `throwOnError`:

```ts
type OpenfortHookOptions<T = { error?: OpenfortError }> = {
  onSuccess?: (data: T) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}
```

* `onSuccess` — Called when the operation succeeds.
* `onError` — Called when the operation fails. Errors are caught and passed here.
* `throwOnError` — When `true`, errors propagate to the nearest React error boundary instead of being passed to `onError`.

```tsx
const { create } = useEthereumEmbeddedWallet()

await create({
  onSuccess: ({ account }) => console.log('Created:', account.id),
  onError: (error) => toast.error(error.message),
})
```
