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

<Meta title='@dxtmisha/en/functional-basic/Classes/ErrorCenter - Error Management'/>

# ErrorCenter Class

`ErrorCenter` is the central tool for managing all errors in the application. It allows you to register possible error causes in one place, configure the reaction to them (e.g., showing notifications), and trigger them from any part of the code.

The class acts as a static bridge (Static Bridge), directing all requests to a single `ErrorCenterInstance`.

## Methods

### Action
- `on(cause: ErrorCenterCauseItem): void` — Starts the processing. Searches the registry, merges data, and calls the appropriate callbacks.

### Verification
- `has(code: string, group?: string): boolean` — Checks if an error is registered.
- `get(code: string, group?: string): ErrorCenterCauseItem | undefined` — Returns error data from the registry.

### Configuration
- `add(cause: ErrorCenterCauseItem): void` — Adds a single error cause.
- `addList(causes: ErrorCenterCauseList): void` — Adds a list of errors (batch registration).
- `addHandler(group: ErrorCenterGroup, handler: ErrorCenterHandlerCallback): void` — Registers a reaction to a group of errors.
- `addHandlerList(handlers: ErrorCenterHandlerList): void` — Batch registration of several handlers.

### Auxiliary
- `getItem(): ErrorCenterInstance` — Returns the internal instance of the class.

## Types

#### `ErrorCenterGroup`
Group name (e.g., 'api', 'auth') or `undefined` for a general handler.

#### `ErrorCenterCauseItem`
Object describing an error: `group`, `code` (required), `priority`, `label`, `message`, `details`.

## Usage Guide

### Step 1: Registration (Registry)
Create an error dictionary to store texts separately from the code.
```typescript
ErrorCenter.add({
  group: 'auth',
  code: 'LOGIN_FAILED',
  label: 'Login Failed',
  message: 'Invalid password.'
})
```

### Step 2: Subscription (Handlers)
Specify how the application should react to an error group.
```typescript
ErrorCenter.addHandler('auth', (cause) => {
  alert(cause.message);
});
```

### Step 3: Execution (Execution)
Trigger the error by its code and group.
```typescript
ErrorCenter.on({ group: 'auth', code: 'LOGIN_FAILED' });
```

## Standard Error Causes

The class already contains pre-defined descriptions for common situations.

#### API Group (`api`)
Used for network request errors.
- **forbidden** — Forbidden (403).
- **notFound** — Resource not found (404).
- **offline** — No internet connection.
- **server** — Internal server error (500).
- **timeout** — Request timeout.
- **unauthorized** — Authorization error (401).
- **unknown** — Unknown error.
- **cacheClear**, **cacheGet**, **cacheRemove**, **cacheSet** — Cache errors.

#### Intl Group (`intl`)
Errors during data formatting (dates, numbers, etc.).
- **datetime**, **display**, **number**, **plural**, **relative**.
- **invalid** — Invalid date or incorrect format.

#### Storage Group (`storage`)
Errors during interaction with local or server storage.
- **context** — Missing context.
- **error** — General read/write failure.

#### Other Groups
These groups use a universal error code `error`. Triggering is done via `ErrorCenter.on({ group: 'group_name', code: 'error' })`.
- **broadcast** — Errors in data exchange between browser tabs.
- **hydration** — Error during data synchronization between server and client.
- **share** — Error when trying to share content (Sharing API).
- **transformation** — Error during data transformation or parsing.

## Examples

### Integration with Notification System
```typescript
import { ErrorCenter } from '@dxtmisha/functional-basic'

// Set up once at app startup
ErrorCenter.addHandler('api', (cause) => {
  Toast.show(cause.label || 'Error', cause.message);
});

// Use in any service
async function loadData() {
  try {
    const data = await fetch('/api');
  } catch (e) {
    ErrorCenter.on({ group: 'api', code: 'server', details: e });
  }
}
```

### Working with Custom Groups
```typescript
// On-the-fly registration
ErrorCenter.on({
  group: 'upload',
  code: 'SIZE_LIMIT',
  label: 'File Too Large',
  message: 'Max 5MB'
});
```
