Development questions

How do you handle errors when calling Apex from LWC and display them to the user?

DevelopmentMedium

Server side: throw an AuraHandledException so the client gets a clean message.

apex
@AuraEnabled
public static void saveData(Account a) {
  try { insert a; }
  catch (DmlException e) { throw new AuraHandledException(e.getMessage()); }
}

Client side: the error object has shape error.body.message (or error.body as an array / pageErrors), so normalise it:

javascript
saveData({ a: this.acc })
  .then(() => this.toast('Saved','success'))
  .catch(e => this.toast('Error', 'error', e.body?.message));

For wire, check the { data, error } result. Also use errorCallback for unexpected rendering errors and lightning-record-edit-form onerror for LDS forms.

lwcerror-handlingapex