Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to detect a new Twilio.Device is created with an expired token. Promise returned when device.connect() never settles, keeps on pending state. #85

Closed
4 of 8 tasks
nnombela opened this issue Mar 30, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@nnombela
Copy link

  • I have verified that the issue occurs with the latest twilio.js release and is not marked as a known issue in the CHANGELOG.md.
  • I reviewed the Common Issues and open GitHub issues and verified that this report represents a potentially new issue.
  • I verified that the Quickstart application works in my environment.
  • I am not sharing any Personally Identifiable Information (PII)
    or sensitive account information (API keys, credentials, etc.) when reporting this issue.

Code to reproduce the issue:

const device = new Twilio.Device(expiredToken)
const neverSettledPromise = device.connect({params})

Expected behavior:

It will raise a TwilioError having the original error '31202, Access Token expired or expiration date invalid' set in field originalError
The returned promise when device.connect() should be settled as rejected when an expired token is used.

Actual behavior:

twilio-2.1.1.js:4453 WebSocket opened successfully.
twilio-2.1.1.js:4453 Setting token and publishing listen
twilio-2.1.1.js:4453 Received error:  ConnectionError: ConnectionError (53000): Raised whenever a signaling connection error occurs that is not covered by a more specific error code.
    at ConnectionError.TwilioError [as constructor] (twilio-2.1.1.js:4041:28)
    at new ConnectionError (twilio-2.1.1.js:3774:32)
    at PStream._handleTransportMessage (twilio-2.1.1.js:5361:33)
    at emitOne (twilio-2.1.1.js:10984:13)
    at WSTransport.emit (twilio-2.1.1.js:11062:7)
    at WebSocket.WSTransport._this._onSocketMessage (twilio-2.1.1.js:9532:19)

Also the returned promise from device.connect() never settles, keeps on pending state.

The problem about the error not having tje orignal error can be solved by incluiding it when creating the TwilioError as shown below, this is an easy fix, no need to create a pull request

  if (type === 'error' && payload.error) {
    payload.error.twilioError = new SignalingErrors.ConnectionError(payload.error);
  }

Software versions:

  • Browser(s):
  • Operating System:
  • twilio.js:
  • Third-party libraries (e.g., Angular, React, etc.):
@nnombela nnombela added the bug Something isn't working label Mar 30, 2022
@mhuynh5757
Copy link
Collaborator

Hi @nnombela , thanks for the bug report. We've filed an internal ticket to fix this issue.

@jbgh2
Copy link

jbgh2 commented Apr 20, 2022

I just ran into this bug via a different route.
My app creates a Device early in its lifecycle and then sometime later make the connect call when the user has decided who to call.

The behavior I expected was that the Device would start the timer to request a new token as soon as it was created (which is implied by the docs) and not after connect is called.

Is the best practice to create a Device and connect almost immediately?

@mhuynh5757
Copy link
Collaborator

Hi all, mentioning a slightly relevant issue #93, the team is tracking this internally with that issue. Will share more in the future.

@charliesantos
Copy link
Collaborator

@jbgh2 you don't need to connect immediately after creating a Device. However, you need to make sure the Device contains a valid token. To detect if the token is expired:

  • When you create a new device, you need to listen to device.on('error') events. The device will emit an AccessTokenExpired error if you initialized it with an expired token
  • You need to listen to tokenWillExpireEvent

@nnombela thank you for reporting. As mentioned above, you can listen to device.on('error') event after you created the device object. An AccessTokenExpired error will be emitted if you provided an expired token.

device.on('error', error => {
  console.log(`${error.message} (${error.code})`);
  // Logs "AccessTokenExpired (20104)"
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants