# HandledError API

A handled error is an error which is handled or thrown by business logic.
It is wanted to expose it the outside world.
Scenarios are input validation failures or "404 Not Found" errors which should be returned to the caller.

---
Canonical: /handbook/api/classes/_purista_core.HandledError/
Source: core/Error/HandledError.impl.ts
Format: Markdown for agents
---

A handled error is an error which is handled or thrown by business logic.
It is wanted to expose it the outside world.
Scenarios are input validation failures or "404 Not Found" errors which should be returned to the caller.

Package: `@purista/core`

## Signature

```typescript
class HandledError
```

## Members

### Constructors

- `new constructor(errorCode: StatusCode, message?: string, data?: unknown, traceId?: string)`

### Properties

- `data: unknown`
- `errorCode: StatusCode`
- `traceId: string`

### Methods

- `getErrorResponse(traceId?: string): Readonly<ErrorResponsePayload>` — Returns error response object
- `toJSON(): { data: unknown; message: string; name: string; stack: string | undefined; status: StatusCode; traceId: string }`
- `toString(): string` — Returns stringified error response object
- `fromError(err: unknown, errorCode?: StatusCode, data?: unknown, traceId?: string): HandledError` — Creates a HandledError from an input.
If the input error is a HandledError it will be returned without modifications.
- `fromMessage(message: Readonly<CommandErrorResponse>): HandledError` — Create a error object from EBMessage error message
