Development Best Practices
  • Home
  • Clean Code
  • Apex
    • Naming Conventions
    • Triggers
    • Unit Testing
    • Principles
      • KISS
      • DRY
      • Single Responsibility
      • Open/Closed
      • YAGNI
    • Patterns
    • SOQL
    • Exception Handling
    • Magic Strings
  • Lightning Components
    • Aura
    • LWC
  • Automation
    • Process Builder
  • Salesforce DX and VSCode
  • Git Source Control
    • Install Git
    • Using Git
    • Development Workflow
      • The Rules
    • Feature Branch Workflow
Powered by GitBook
On this page
  • Aura Exception Handling
  • Usages
  • Basic Aura Handled Exception Factory class
  • Advanced Aura Handled Exception Factory class
  • Exception Handler
  • Dml Exception Handler
  1. Apex

Exception Handling

Aura Exception Handling

Using the Aura exception handling factory enables developer/user friendly error messages based on the exception type.

Usages

    @AuraEnabled
    public static String getSomething()
    {
        try {
        
            // do something
            
        } catch (Exception cause) {
           throw AuraHandledExceptionFactory.create(cause);
        }
    }
    

Basic Aura Handled Exception Factory class

Basic solution enables developer friendly error messages when unit testing.

public class AuraHandledExceptionFactory {
    
    public static AuraHandledException create(Exception cause) {

        AuraHandledException error = new AuraHandledException(cause.getMessage());
        error.setMessage(cause.getMessage());
        return error;
    }
}

Advanced Aura Handled Exception Factory class

Following the Open/Closed Principle this solution allows developers to implement custom error message responses based on the exception type.

public class AuraHandledExceptionFactory {
    
    public static AuraHandledException create(Exception cause) {
        Type handlerType = getType(cause);
        ExceptionHandler handler = newInstance(handlerType);
        String message = handler.getMessage(cause);
        AuraHandledException error = new AuraHandledException(message);
        error.setMessage(message);
        return error;
    }

    private static ExceptionHandler newInstance(Type handlerType) {
        try {
            return (ExceptionHandler) handlerType.newInstance();
        } catch (Exception ignore) {
            return new ExceptionHandler();
        }
    }

    private static Type getType(Exception cause) {
        return Type.forName(getTypeName(cause));
    }

    private static String getTypeName(Exception cause) {
        return cause.getTypeName() + 'Handler';
    }
}

Exception Handler

public virtual class ExceptionHandler {
    public virtual String getMessage(Exception cause) {
        return cause.getMessage();
    }
}

Dml Exception Handler

public class DmlExceptionHandler extends ExceptionHandler {

    public override String getMessage(Exception cause) {

        DmlException dml = (DmlException) cause;
        
        // create user friendly error message

        String message = ''; 

        for(integer index = 0; index < dml.getNumDML(); index++) {
            message += dml.getDmlMessage(index);
        }
        return message;
    }
}
PreviousSOQLNextMagic Strings

Last updated 5 years ago