Promise Rejection Patterns
All asynchronous operations in the React Native SSH SFTP library return Promises that reject when errors occur. This provides a consistent error handling pattern across the API.Basic Error Handling
Use try/catch blocks with async/await:.catch() with Promises:
Error Types
The library uses a generic error type from the native modules:- Connection errors: Network issues, authentication failures, timeouts
- SFTP errors: File not found, permission denied, disk full
- SSH command errors: Command execution failures, shell errors
- Native module errors: Platform-specific errors from iOS/Android implementations
Common Errors
Authentication Errors
Occur when credentials are invalid or key-based authentication fails:Connection Errors
Occur when the client cannot establish a connection:SFTP Errors
Occur during file transfer operations:Command Execution Errors
Occur when SSH commands fail:Error Handling Best Practices
1. Always Handle Errors
2. Clean Up Resources on Error
Always disconnect clients, even when errors occur:3. Separate Connection and Operation Errors
Handle connection errors separately from operation errors:4. Provide User-Friendly Error Messages
Translate technical errors into user-friendly messages:5. Implement Retry Logic
For transient errors, implement retry with exponential backoff:6. Handle File Transfer Cancellation
Properly handle cancelled uploads and downloads:7. Log Errors for Debugging
Implement comprehensive error logging:Using Callbacks (Alternative Pattern)
The library also supports callback-based error handling:While callbacks are supported, using async/await with try/catch is the recommended approach for cleaner, more maintainable code.
React Native Specific Considerations
Network State
Check network connectivity before attempting connections:Error Boundaries
Use React Error Boundaries to catch errors in components:Platform-Specific Errors
iOS Simulator Limitation
Next Steps
- Review Authentication for credential handling
- Learn about Connection Management for proper lifecycle management
- Explore the API Reference for complete method documentation