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
Always log exceptions: Never let exceptions go unlogged
Include context: Add relevant business context to error logs
Use appropriate levels: Use ERROR level for exceptions
Flush immediately: Use
log()instead ofaddLog()for errorsPreserve stack traces: Include full exception information
Add business context: Include relevant IDs, operation details, and state
Exception Handling Patterns
Specific exception types: Handle different exception types appropriately
Graceful degradation: Implement fallback behavior when possible
User-friendly messages: Provide meaningful error messages to users
Recovery strategies: Implement retry logic for transient failures
Monitoring integration: Ensure errors are visible in monitoring systems
Debugging Support
Rich context: Include all relevant information for debugging
Performance metrics: Track timing and resource usage
State preservation: Log system state at time of error
Correlation IDs: Use transaction IDs to correlate related errors
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