[SalesForce] Display Apex Exception message nicely in Lightning toast

My Apex controller method might throw a custom exception

@AuraEnabled
public static Id myAction() {
    // Provoke exception
    Integer foo = 10 / 0;

and I want the toast to display it properly

var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
     title: 'Error',
     type: 'error',
     message: response.getError()[0]
});
toastEvent.fire();

Instead of my the string I passed into my custom exception I get this! What am I doing wrong?

enter image description here

Best Answer

Adding ugly wrapping code to all my @AuraEnabled methods seemed not to be an acceptable clean solution to me...

@AuraEnabled
public static Id myAction() {
   try {
      // regular action code
   }
   catch (Exception ex){
      throw new AuraHandledException(ex.getMessage());
   }
 }

...so I solved it in my JS toast code.

var uglyIndicator = "Caused by: common.apex.runtime.impl.ExecutionException:";
if(message.includes(uglyIndicator)) {
    message = message.split(uglyIndicator)[1];
}