# Error Handling

> The library provides built-in error tracking at multiple levels, reporting client-side errors back to the Segmentify API.

## The ErrorHandler Event

Send error information explicitly via `SegmentifyEventManager().ErrorHandler`:

```typescript
try {
  await SegmentifyEventManager().ProductView(payload);
} catch (error) {
  await SegmentifyEventManager().ErrorHandler(error as Error);
}
```

This sends a `CUSTOM_EVENT` with type `error_handler` containing the error message.

## Internal Error Handling

The library catches errors internally so a single failure doesn't break the integration:

- **Initializer** — wraps setup steps in try-catch. Logs the error, sends an `ErrorHandler` event, and continues for non-critical failures.
- **Events** — if a single event fails (network error, bad response), it's skipped and the [queue](/10-queue) continues processing.
- **Page Detection** — if `preExecuteFunction` or `postExecuteFunction` throws, the error is caught and logged.

## Best Practices

1. **Wrap custom callbacks in try-catch** — handle errors in your `preExecuteFunction` / `postExecuteFunction` to prevent breaking page detection.
2. **Use debug mode during development** — set `debug: true` to see error traces in the console (see [Logger](/11-logger)).
3. **Monitor ErrorHandler events** — check your Segmentify dashboard for `CUSTOM_EVENT` entries with type `error_handler`.

## See Also

- [Events](/05-events)
- [Logger](/11-logger)
