# How should developers handle and parse error objects returned by wired data adapters in LWC?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Error Handling, Wire Service

Read it on the site: https://www.forcechallenges.com/interview-questions/how-should-developers-handle-and-parse-error-objects-returned-by-wired-data-adapters-in-lwc

## Answer

**Basics:** A wired property resolves to `{ data, error }`; exactly one is populated. For imperative Apex use `try/catch` or `.catch`. The error typically has `status` (HTTP code), `statusText` and `body`. For Apex exceptions `body.message` holds the text; for UI API (LDS) errors `body` can be an array of objects or an object with `message`, `output.errors` and `output.fieldErrors`.

**How it works:** In a wire handler, check `error`, convert it to a readable string, store it, and show it with `lightning-card`, an inline message or `ShowToastEvent`. For `lightning-record-edit-form`, `onerror` gives `event.detail.message` and `event.detail.detail`. For field-level problems in `updateRecord` use `error.body.output.fieldErrors`.

**Advanced / deep dive:** Throw `AuraHandledException` from Apex and call `setMessage()` so the client gets a clean, user-safe message; other exceptions arrive as generic text or expose stack details. Distinguish categories: network failure (status 0 or missing), permission or sharing problems (403 or `INSUFFICIENT_ACCESS`), validation failures (`FIELD_CUSTOM_VALIDATION_EXCEPTION`), and concurrency (`UNABLE_TO_LOCK_ROW`). Do not show raw technical messages to end users; log them (for example via a platform event or custom log object) and show friendly text. For wires, remember an error replaces `data` with `undefined`, so templates must handle both states. In Jest, simulate with `adapter.error()`.

**Code / config example:**
```javascript
export function reduceErrors(errors) {
    if (!Array.isArray(errors)) errors = [errors];
    return errors
        .filter(e => !!e)
        .map(e => {
            if (Array.isArray(e.body)) return e.body.map(b => b.message);
            if (e.body && typeof e.body.message === 'string') return e.body.message;
            if (typeof e.message === 'string') return e.message;
            return e.statusText;
        })
        .flat()
        .filter(m => !!m);
}
// usage
@wire(getRecord, { recordId: '$recordId', fields: FIELDS })
wired({ data, error }) {
    if (data) { this.record = data; this.errorText = undefined; }
    else if (error) { this.record = undefined; this.errorText = reduceErrors(error).join(', '); }
}
```

**Hidden gotchas & must-remember points:**
- `error.body` can be an array, an object, or missing; never assume `body.message`.
- Only `AuraHandledException` messages are reliably user-friendly.
- A wire error clears `data`; handle the empty state.
- Field-level errors live under `body.output.fieldErrors`.
- Errors are not toasts; you must display them.
- Check `status` for 0/offline cases rather than parsing text.

**Best practice:** Centralize error normalization in one shared utility and always render a visible error state.

---
Source: ForceChallenges · last updated 2026-11-02
