# Utilities

In a widget built from the canonical template, utilities without a domain live in `src/shared/lib/`. The template ships two: `errorHandler.ts` and `liquidParser.ts`.

| Where | What goes there |
|-------|-----------------|
| `src/shared/lib/` | Utilities with no domain, such as error handling and the Liquid parser |
| `src/features/<name>/model/` | Pure functions of one domain, such as the DTO → domain mapper (`modyo://docs/widgets/patterns-features`) |
| `src/shared/hooks/` | Generic React hooks (`modyo://docs/widgets/patterns-hooks`) |

`src/shared/` never imports features, `src/app/` or `src/pages/`; ESLint enforces it.

---

## `errorHandler.ts`

`src/shared/lib/errorHandler.ts` is a default export. It ignores aborted requests (`AbortError`) and logs everything else:

```ts
/* eslint-disable @typescript-eslint/no-explicit-any */
export default function errorHandler(error: any) {
  if (error.name === 'AbortError') {
    return;
  }

  console.log(error);
}
```

Import it as a default import:

```ts
import errorHandler from '@/shared/lib/errorHandler';
```

The validator requires the file (*Core Files*) and checks that it has an export (*Error Handler Export*, warning).

---

## `liquidParser.ts`

`src/shared/lib/liquidParser.ts` resolves Liquid tags such as `{{site.language}}` or `{{vars.api-base-url}}`:

- in development, after `src/config/liquidConfig.ts` initializes it with the values of `src/config/liquid.json`, it renders the tag with `liquidjs`;
- in a build it is never initialized, so `parse()` returns the tag untouched and Modyo resolves it on the server.

```ts
  parse(liquidString: string): string {
    if (this.engine) {
      return this.parseLiquid(liquidString);
    }
    return liquidString;
  },
```

Do not call it across the code: read each site variable once in `src/config/widgetConfig.ts` and import the constant from there. Details: `modyo://docs/widgets/patterns-liquid-environment`.

---

## Adding a utility

- A function with no domain that several features use goes in `src/shared/lib/<name>.ts`.
- A function of one domain stays in that feature's `model/` (pure) or `service/` (orchestration).
- `src/shared/lib/` does not import features, `src/app/` or `src/pages/`.

---

## See also

- `modyo://docs/widgets/reference-architecture` — project structure and import graph
- `modyo://docs/widgets/patterns-liquid-environment` — Liquid variables
