Error Handling Architecture
The Flutter SDK implements a structured approach to error handling through several key components:- OnSocketErrorReceived Callback: Defines a callback function that is triggered when socket errors occur
- TelnyxSocketError Class: Provides a data structure for encapsulating error states
- TelnyxErrorConstants Class: Defines standard error codes and messages used throughout the SDK
- Network Connectivity Monitoring: Detects and handles network changes and connectivity issues
- Gateway State Management: Monitors the gateway registration status and triggers errors when appropriate
Error Scenarios
1. Gateway Registration Status
The SDK monitors the gateway registration status and triggers errors in the following scenarios:- When the gateway status is “FAILED” after registration attempts
- When the gateway registration times out
- Location:
telnyx_client.dart - This ensures that the client is properly connected to the Telnyx network
2. Gateway Registration Timeout
The SDK handles gateway registration timeout:- When the gateway registration process takes too long to complete
- Location:
telnyx_client.dart - This helps applications handle situations where the gateway is unreachable
3. WebSocket Error Messages
The SDK handles error messages received through the WebSocket connection:- When the server sends an error message via WebSocket
- Location:
telnyx_client.dart-_onMessagemethod - These errors typically indicate issues with the connection or server-side problems
4. Network Connectivity Issues
The SDK detects network connectivity problems and reports them as errors:- When network is lost during an active session
- When network type changes (e.g., from WiFi to mobile data)
- Location:
telnyx_client.dart-_handleNetworkLostmethod - These errors help applications handle offline scenarios gracefully
TelnyxSocketError Implementation
TheTelnyxSocketError class provides a standardized way to handle errors throughout the SDK:
- An error code that identifies the type of error
- An error message describing what went wrong
Error Constants
The SDK defines standard error constants in theTelnyxErrorConstants class:
Call State Error Handling
The SDK uses theCallState enum to track the state of calls, including error states:
dropped or reconnecting states with an associated NetworkReason:
Consuming Errors in Your Application
To handle errors in your application, you should implement theonSocketErrorReceived callback:
Error Handling Best Practices
When implementing error handling for the Telnyx WebRTC Flutter SDK:- Always implement the onSocketErrorReceived callback: This is the primary channel for receiving error notifications
- Log errors for debugging purposes: Capture error messages for troubleshooting
- Implement appropriate error recovery mechanisms: Different errors may require different recovery strategies
- Display user-friendly error messages: Translate technical error messages into user-friendly notifications
- Implement reconnection logic when appropriate: For network or gateway issues, automatic reconnection may be appropriate
- Monitor call state changes: Watch for
dropped,reconnecting, anderrorstates to handle call-specific issues
Common Error Scenarios and Solutions
Gateway Registration Failure
- Cause: Network connectivity issues or invalid credentials
- Solution: Check network connection and credential validity, then attempt reconnection
Gateway Registration Timeout
- Cause: Server unreachable or network latency issues
- Solution: Implement retry mechanism with exponential backoff
WebSocket Connection Errors
- Cause: Network interruption or server issues
- Solution: Implement automatic reconnection with exponential backoff
Network Connectivity Changes
- Cause: Device switching between WiFi and mobile data, or losing connectivity
- Solution: Monitor network state changes and implement appropriate UI feedback and reconnection logic