import {Meta} from '@storybook/addon-docs/blocks'

<Meta title='@dxtmisha/en/functional-basic/Classes/ErrorCenterHandler - Handler Management'/>

# ErrorCenterHandler Class

`ErrorCenterHandler` is a manager for handling callback functions (handlers). It allows you to bind logic to specific error groups and execute it when a corresponding event occurs.

## Key Features

- **Handler Registration** — convenient addition of callback functions to specific groups.
- **Global Handler** — support for the `undefined` group, which catches any error without a specific handler.
- **Sequential Execution** — triggers all registered functions in the order they were added.
- **Integrated Logging** — automatically outputs error details to the console.

## Initialization

To create an instance, use the constructor `ErrorCenterHandler(handlers)`.

**Parameters:**
- `handlers?: ErrorCenterHandlerList` — initial list of handlers.

```typescript
import { ErrorCenterHandler } from '@dxtmisha/functional-basic'

const handlers = new ErrorCenterHandler([
  {
    group: 'api',
    handlers: [(cause) => console.log('API Error:', cause.code)]
  }
]);
```

## Methods

### Action
- `on(cause: ErrorCenterCauseItem): this` — Triggers the chain of handlers. Always outputs an error to the console via `console.error`.

### Verification
- `has(group: ErrorCenterGroup): boolean` — Checks if there are handlers for a group.
- `get(group: ErrorCenterGroup): ErrorCenterHandlerItem | undefined` — Returns a list of functions for a group.

### Configuration
- `add(group: ErrorCenterGroup, handler: ErrorCenterHandlerCallback): this` — Adds a single function to a group.
- `addList(handlers: ErrorCenterHandlerList): this` — Batch registration of handlers.

## Types

#### `ErrorCenterGroup`
Group name (`string`) or `undefined` for global interception.

#### `ErrorCenterHandlerCallback`
Callback function: `(cause: ErrorCenterCauseItem) => void`.

#### `ErrorCenterHandlerItem`
Link between a group and an array of its functions.
- `group?: ErrorCenterGroup` — target group.
- `handlers: ErrorCenterHandlerCallback[]` — list of functions.

## Examples

### Isolated Manager for a Module
```typescript
const uploadHandlers = new ErrorCenterHandler();

uploadHandlers.add('upload', (cause) => {
  if (cause.code === 'retry') {
    restartUpload();
  }
});

uploadHandlers.on({ group: 'upload', code: 'retry' });
```
