I call an Apex method statically.
@AuraEnabled
public static Id insertx(){
insert new Account();
}
From a LWC component:
insertx().then( () => {})
.catch(error => {this.error = error;})
My problem is that the message returned is:
"An internal server error has occurred Error ID: 830852859-43925 (1007478720)"
Anyone knows how to get a proper message from Apex?
I also tried using a try/catch and throwing an AuraException but it gave the same result
try{
// a required field is missing
insert new Account();
}
catch(Exception e){
Database.rollback(sp);
System.debug(Logginglevel.ERROR, e.getMessage());
throw new AuraException(e.getMessage());
}
Best Answer
You don't have to throw
AuraException
, instead, you have to throwAuraHandledException (for Aura components only)
AuraHandledException (for LWC components)
In Console:
{"ok":false,"status":400,"statusText":"Bad Request","body":{"message":"Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account Name]: [Account Name]"}}
Throwing any exception other than
AuraHandledException
gives internal server error.