Aleksey  Voitovich
Aleksey Voitovich

Reputation: 21

Twilio SDK General error #31000

We use Twilio SDK in our iOS app. It works fine but sometimes didStopListeningForIncomingConnections callback is called with error=31000 ("General error"). After that, the device turns to a strange state: it seems to be online but it's impossible to call it. And it shows "unconnected" state on the device.

So the questions are: 1. What does this 31000 error means? 2. What should we do in such a case? How to reconnect device to Twilio?

Upvotes: 2

Views: 2819

Answers (2)

Joseph Viscomi
Joseph Viscomi

Reputation: 31

This may or may not be the issue, we have encountered 31000 errors two times in our development and both were a result of generating the JWT on our server api. To be clear the error was a 31000 on the client, but the reason for this was in the construction of the JWT, and the params we wanted twilio to send back to our application.

When passing in an object to allow_client_outgoing or allow_client_incoming the twilio sdk concats this all in their scope attribute in their JWT. It added it to the scope:client:outgoing?appSid= which looks like a query string. That means it has a size limit of 2048. So exceeding this length generates a 31000 error.

In addition adding the objects doesn't seem to always implicitly serialize the object correctly, it introduces characters that can generate errors in their corresponding mobile sdks (but not their web sdk ... weird) so we took care of this by explicitly serializing objects to JSON before they are inserted into the JWT.

I hope both of these examples help you track down the issue.

Upvotes: 0

Megan Speir
Megan Speir

Reputation: 3811

Megan from Twilio here.

  1. You can see what an error for Twilio Client means here: https://www.twilio.com/docs/api/client/errors

However, 31000 is a rather vague and less than ideal error message as you describe. In this case, it is likely that the Twilio capability token has probably expired while the application is in the background, and if you merely call the listen method whenever they are receiving the 31000 generic error, it might cause the client SDK to result in a error-retry loop and crash the application eventually.

At the time of your writing with TwilioClient iOS SDK v1.2.5, it is suggested to use the following sample code in your did-stop-listening callback:

- (void)device:(TCDevice*)device didStopListeningForIncomingConnections:(NSError*)error {
    if ( [self checkCapabilityTokenStillValid] ) {
        // if the token has not yet expired, use the `listen` method of `TCDevice` to resume the listening state
        [self.device listen];
    }
    else {
        // restart all over by requesting a new capability token and initialize/update the `TCDevice` instance again
        [self login];
    }
}

The TwilioClient iOS SDK takes care of dispatching the listen and updateCapabilitiyToken: methods to the current thread for execution, therefore it's safe to call them directly in the didStopListeningForIncomingConnections. The did-stop-listening delegate method is always triggered with dispatch_get_main_queue() of Grand Central Dispatch.

Hope this may help others if they run into the same generic error.

Upvotes: 2

Related Questions