I love @wire
(ing) LWC properties like
import { LightningElement, wire } from "lwc";
import getAccounts from "@salesforce/apex/MyCtrl.getAccounts";
export default class MyComponent extends LightningElement {
@wire(getAccounts)
accounts = [];
}
to a @AuraEnabled(cachable=true)
Apex method
public with sharing class MyCtrl {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
try {
return getViaCallout();
}
catch(Exception ex) {
new ApplicationException().throwAuraHandled(ex);
}
return null;
}
}
But I am unsure about how to handle errors gracefully. E.g. if an exception is thrown in Apex and rethrown as AuraHandledException
. How do I catch and display it as an LWC standard toast?
If I would not use @wire I would do:
loadAccounts() {
getAccounts()
.then((result) => {
this.accounts = result;
})
.catch((error) => {
this.toastError(error, "Appway: Unable to create process");
});
}
toastError(error, title) {
this.dispatchEvent(
new ShowToastEvent({
title: title,
message: reduceErrors(error).join(", "),
variant: "error"
})
);
}