Error Handling Patterns
Understanding Error Codes
React Native ExecuTorch uses typed error codes for different failure scenarios. All errors inherit fromRnExecutorchError.
Common Error Codes
From/packages/react-native-executorch/src/errors/ErrorCodes.ts:4:
| Code | Name | Description |
|---|---|---|
| 101 | UnknownError | Unexpected error |
| 102 | ModuleNotLoaded | Model not downloaded/loaded |
| 103 | FileWriteFailed | Failed to save file |
| 104 | ModelGenerating | Model already processing |
| 105 | LanguageNotSupported | Unsupported language |
| 112 | InvalidConfig | Invalid configuration |
| 113 | ThreadPoolError | Threading issue |
| 114 | FileReadFailed | Failed to read file |
| 115 | InvalidModelOutput | Unexpected output |
| 116 | WrongDimensions | Input dimension mismatch |
| 117 | InvalidUserInput | Invalid input data |
| 118 | DownloadInterrupted | Download interrupted |
| 167 | TokenizerError | Tokenization failed |
| 180 | ResourceFetcherDownloadFailed | Download failed |
| 186 | ResourceFetcherAdapterNotInitialized | Fetcher not initialized |
ExecuTorch Runtime Errors
Errors from the ExecuTorch runtime:| Code | Name | Description |
|---|---|---|
| 0 | Ok | Success |
| 1 | Internal | Internal error |
| 2 | InvalidState | Invalid executor state |
| 16 | NotSupported | Operation not supported |
| 20 | OperatorMissing | Missing operator |
| 33 | MemoryAllocationFailed | OOM |
| 35 | InvalidProgram | Invalid .pte file |
| 48-50 | Delegate* | Backend delegation errors |
Debugging with Hooks
Monitor Hook State
Track Token Generation
Debugging with TypeScript API
Custom Callbacks
Common Issues and Solutions
1. Model Not Loading
Symptom:ModuleNotLoaded error or isReady stays false
Causes:
- Download failed
- Invalid model URL
- Resource fetcher not initialized
- Insufficient storage space
2. Out of Memory Errors
Symptom:MemoryAllocationFailed error, app crashes
Debug:
3. Generation Hangs or Fails
Symptom:generate() never completes, isGenerating stuck at true
Debug:
4. Invalid Input Errors
Symptom:InvalidUserInput or WrongDimensions
Debug:
5. Tokenizer Errors
Symptom:TokenizerError during LLM operations
Debug:
6. Download Failures
Symptom:ResourceFetcherDownloadFailed
Debug:
Debugging Model Outputs
Inspect LLM Responses
Debug Computer Vision Outputs
Platform-Specific Debugging
iOS Debug Logs
- Open
.xcworkspacein Xcode - Run app
- View logs in Console pane
Android Debug Logs
Performance Debugging
Profile Generation Speed
Monitor Resource Usage
Testing Strategies
Unit Testing Model Integration
Best Practices
- Always Check
isReady: Before using models - Monitor
errorState: React to errors in real-time - Implement Timeouts: Prevent hanging operations
- Log Comprehensively: Track state changes and errors
- Test on Real Devices: Emulators may hide issues
- Handle All Error Codes: Provide specific error messages
- Profile Performance: Monitor token generation speed
- Validate Inputs: Check data before passing to models
Debugging Checklist
When encountering issues:- Is resource fetcher initialized?
- Is model downloaded? Check
downloadProgress - Is model loaded? Check
isReady - Are there any errors? Check
errorstate - Are inputs valid? Validate before processing
- Is device memory sufficient? See Memory Management
- Are you handling the correct error codes?
- Have you tested on a physical device?
Next Steps
- Review Troubleshooting Guide for specific issues
- Learn about Memory Management
- Check Performance Optimization