Sub topic 10
Handling errors and partial data
This chapter assumes you can write a graphql wire query, read edges and node, and pass reactive variables. Here you will learn how the adapter reports problems and how to write a handler that copes with them.
How the adapter reports problems
The graphql wire adapter provisions an object with data and errors. Both can be present at the same time. GraphQL allows a query to succeed for some fields and fail for others, so a result can hold usable data and a list of problems together.
| Situation | `data` | `errors` |
|---|---|---|
| Full success | Populated | `undefined` |
| Query cannot run (syntax, unknown field) | `undefined` | Populated |
| Some fields or records fail | Populated, with `null` in failed spots | Populated |
| Wire has not yet responded | `undefined` | `undefined` |
The last row matters. A handler that treats "no data and no errors" as a failure will flash an error message on every load.
Check both properties
Do not write if (data) { ... } else { ... } and assume the else branch means an error. Test each property on its own.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class SafeAccountList extends LightningElement {
accounts = [];
errors;
loading = true;
@wire(graphql, {
query: gql`
query safeAccounts {
uiapi {
query {
Account(first: 10, orderBy: { Name: { order: ASC } }) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}
`
})
wiredAccounts({ data, errors }) {
if (data) {
this.loading = false;
this.accounts = data.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name?.value ?? '(no name)'
}));
}
if (errors) {
this.loading = false;
this.errors = errors;
} else if (data) {
this.errors = undefined;
}
}
get errorMessages() {
return (this.errors ?? []).map((error) => error.message);
}
}Each entry in errors has a message string. Show these to developers in logs. Show users a short, plain sentence instead.
Partial results
When one field fails, for example because the user lacks field-level security on it, the adapter can return the rest of the record. The failed field comes back as null. That is why the example uses node.Name?.value. Reading node.Name.value on a null field throws a TypeError and the whole component breaks.
Apply the same care to every field you read:
- Use optional chaining (
?.) on each wrapper before readingvalueordisplayValue. - Supply a fallback with
??where the template needs text. - Decide whether a record with missing fields should still be shown. For a list, usually yes. For a record page that depends on one key field, you might show an error instead.
Keep the old data on a failed rerun
When a reactive variable changes and the new query fails, your handler runs again with errors. Decide what the user sees. Clearing accounts shows an empty list. Keeping the previous list shows stale data beside an error message. In the earlier example, only the error state changes when data is missing, so the last good list stays. Choose this deliberately and tell the user, for instance with a banner above the list.
Showing the error in the template
<template>
<lightning-card title="Accounts" icon-name="standard:account">
<template lwc:if={loading}>
<lightning-spinner alternative-text="Loading"></lightning-spinner>
</template>
<template lwc:if={errors}>
<p class="slds-p-around_medium">Some account data could not be loaded.</p>
</template>
<ul class="slds-p-around_medium">
<template for:each={accounts} for:item="account">
<li key={account.id}>{account.name}</li>
</template>
</ul>
</lightning-card>
</template>The message is generic on purpose. The list still renders whatever arrived.
Flow of a result
Common mistakes
- Treating
elseafterif (data)as the error branch. - Reading
node.Field.valuewithout optional chaining. - Showing raw
error.messagetext to end users. - Never clearing
errorsafter a later successful run, so the message sticks.
Exercise
A query returns both data and a non-empty errors array, and one record has Industry: null. What should your handler do, and which expression avoids a crash when reading that field?
Show answer
Render the records that arrived and also show an error message, instead of discarding data. Read the field with optional chaining and a fallback, such as node.Industry?.displayValue ?? node.Industry?.value ?? '', so a null Industry does not throw a TypeError.