Exception Handling

Best practices for error logging and debugging with Triton, including exception handling patterns and debugging strategies.

Best Practices for Error Logging

Effective exception handling is crucial for debugging and monitoring. Triton provides powerful capabilities for capturing comprehensive error information.

Exception Logging Patterns

Basic Exception Logging

try {
    // Business logic
    processAccount(account);
} catch (Exception e) {
    // Always log errors
    Triton.instance.addError(TritonTypes.Area.Accounts, e);
}

Advanced Exception Logging with Context

public class ExceptionHandlingService {
    public static void processWithExceptionHandling(Id recordId) {
        // Set up template for error handling
        Triton.instance.setTemplate(
            Triton.makeBuilder()
                .category(TritonTypes.Category.Apex)
                .type(TritonTypes.Type.Backend)
                .area(TritonTypes.Area.Accounts)
                .relatedObject(recordId)
        );
        
        try {
            // Business logic
            processRecord(recordId);
            
        } catch (DmlException e) {
            // Handle DML-specific errors
            Triton.instance.log(
                Triton.instance.fromTemplate()
                    .exception(e)
                    .summary('DML operation failed')
                    .details('recordId=' + recordId + ', operation=update')
            );
            throw e;
            
        } catch (CalloutException e) {
            // Handle callout-specific errors
            Triton.instance.log(
                Triton.instance.fromTemplate()
                    .exception(e)
                    .summary('External API call failed')
                    .details('recordId=' + recordId + ', endpoint=external-service')
            );
            throw e;
            
        } catch (Exception e) {
            // Handle all other errors
            Triton.instance.log(
                Triton.instance.fromTemplate()
                    .exception(e)
                    .summary('Unexpected error occurred')
                    .details('recordId=' + recordId + ', context=record-processing')
            );
            throw e;
        }
    }
}

Real-World Exception Handling Example

LWC Exception Handling

Exception Types and Handling

DML Exceptions

Callout Exceptions

Custom Exceptions

Debugging Strategies

Stack Trace Analysis

Performance Monitoring

Best Practices

Error Logging Guidelines

  1. Always log exceptions: Never let exceptions go unlogged

  2. Include context: Add relevant business context to error logs

  3. Use appropriate levels: Use ERROR level for exceptions

  4. Flush immediately: Use log() instead of addLog() for errors

  5. Preserve stack traces: Include full exception information

  6. Add business context: Include relevant IDs, operation details, and state

Exception Handling Patterns

  1. Specific exception types: Handle different exception types appropriately

  2. Graceful degradation: Implement fallback behavior when possible

  3. User-friendly messages: Provide meaningful error messages to users

  4. Recovery strategies: Implement retry logic for transient failures

  5. Monitoring integration: Ensure errors are visible in monitoring systems

Debugging Support

  1. Rich context: Include all relevant information for debugging

  2. Performance metrics: Track timing and resource usage

  3. State preservation: Log system state at time of error

  4. Correlation IDs: Use transaction IDs to correlate related errors

  5. Progressive detail: Start with high-level errors, add detail as needed

By following these exception handling best practices, you'll create robust error logging that enables effective debugging and monitoring across your Salesforce org.

Last updated