WebRTC JS SDK Error Handling
This document provides a comprehensive overview of error handling in the Telnyx WebRTC JS SDK, including when thetelnyx.notification event is triggered, the types of errors that can occur, and how the SDK handles reconnection attempts.
Table of Contents
- Introduction
- Error Constants Reference
- Call Termination Reasons
- The telnyx.notification Event Handler
- Error Types
- Reconnection Process
- Best Practices
Introduction
The Telnyx WebRTC JS SDK provides robust error handling mechanisms to help developers manage various error scenarios that may occur during the lifecycle of a WebRTC connection. Understanding these error handling mechanisms is crucial for building reliable applications that can gracefully recover from failures.Error Constants Reference
The following table lists all error constants and codes used in the Telnyx WebRTC JS SDK:| ERROR MESSAGE | ERROR CODE | DESCRIPTION |
|---|---|---|
| Token registration error | -32000 | Error during token registration |
| Credential registration error | -32001 | Error during credential registration |
| Codec error | -32002 | Error related to codec operation |
| Gateway registration timeout | -32003 | Gateway registration timed out |
| Gateway registration failed | -32004 | Gateway registration failed |
| Call not found | N/A | The specified call cannot be found |
| User media error | N/A | Browser does not have permission to access media devices |
| Connection timeout | -329990 | Fake verto timeout error code |
Call Termination Reasons
The SDK provides detailed information about why a call has ended through the call’scause and causeCode properties. These properties are available when a call reaches the hangup state.
Call Termination Fields
| FIELD | TYPE | DESCRIPTION |
|---|---|---|
cause | string | General cause description (e.g., “CALL_REJECTED”, “USER_BUSY”) |
causeCode | number | Numerical code for the cause (e.g., 21 for CALL_REJECTED) |
sipCode | number | SIP response code (e.g., 403, 404) |
sipReason | string | SIP reason phrase (e.g., “Forbidden”, “Not Found”) |
Common Cause Values
| CAUSE | DESCRIPTION |
|---|---|
NORMAL_CLEARING | Normal call termination |
USER_BUSY | The remote user is busy |
CALL_REJECTED | The call was rejected by the remote party |
UNALLOCATED_NUMBER | The dialed number is invalid or does not exist |
NO_ANSWER | The call was not answered |
PURGE | Call was purged from the system |
Example Usage
The telnyx.notification Event Handler
Thetelnyx.notification event is the primary mechanism for receiving error notifications and call state updates in the JS SDK. This event provides a way for your application to be notified of errors and take appropriate action.
Event Structure
When is telnyx.notification Triggered?
Thetelnyx.notification event is triggered in the following scenarios:
-
Call State Updates: When a call changes state (ringing, active, hangup, etc.)
- Event Type:
callUpdate - Contains:
callobject with current state and termination details
- Event Type:
-
User Media Errors: When the browser cannot access media devices
- Event Type:
userMediaError - Contains:
errorobject with details about the media access failure
- Event Type:
-
Connection State Changes: When the client connection state changes
- Event Type:
vertoClientReady - Indicates the client is ready to make/receive calls
- Event Type:
Example Implementation
Error Types
The SDK encounters different types of errors that require different handling approaches.User Media Errors
User media errors occur when the browser cannot access the user’s microphone or camera. Common Causes:- User denied permission to access media devices
- Media devices are not available or in use by another application
- Browser security restrictions (HTTPS required for media access)
- Invalid audio/video constraints
Call State Errors
Call state errors are indicated through the call’s state transitions and termination reasons. Call States:new: New call has been createdtrying: Attempting to establish the callrequesting: The outbound call is being sent to the serverringing: Incoming call or outbound call is ringinganswering: Attempting to answer an incoming callearly: Receiving media before the call is answeredactive: Call is connected and activeheld: Call is on holdhangup: Call has endeddestroy: Call object is being destroyedpurge: Call has been purged from the system
Connection Errors
Connection errors occur when there are issues with the WebSocket connection to the Telnyx servers. Common Connection Issues:- Network connectivity problems
- Authentication failures
- Server-side issues
- Firewall or proxy blocking WebSocket connections
Reconnection Process
The SDK includes automatic reconnection mechanisms to handle temporary network issues:Automatic Reconnection
- Connection Monitoring: The SDK monitors the WebSocket connection status
- Automatic Retry: When a connection is lost, the SDK attempts to reconnect automatically
- Exponential Backoff: Retry intervals increase progressively to avoid overwhelming the server
- Call Recovery: Active calls may be recovered if the connection is restored quickly