Skip to main content

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:
Or use .catch() with Promises:

Error Types

The library uses a generic error type from the native modules:
Errors can originate from:
  1. Connection errors: Network issues, authentication failures, timeouts
  2. SFTP errors: File not found, permission denied, disk full
  3. SSH command errors: Command execution failures, shell errors
  4. 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

Never ignore Promise rejections. Always implement error handling for all asynchronous operations.

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

The library does not currently support the iOS simulator. Always test on physical iOS devices.

Next Steps

Last modified on March 26, 2026