microsoft / microsoft/BotFramework-DirectLineJS

Application crashes when refresh token returns an error

Open
#421 3 comments 0 reactions 1 assignee View on GitHub

@compulim is already working on this.

Since Jul 24, 2024.

bug
Dominant language
TypeScript
Stars
199
Forks
133
PR merge metrics
No merged PRs in 30d

Description

If tokens/refresh returns an error, the application crashes, and there is no way to catch and handle it properly.

Reproduced by:

import { DirectLine, ConnectionStatus } from 'botframework-directlinejs';
import WebSocket, { WebSocketServer } from 'ws';
import nock from 'nock';
import xhr2 from 'xhr2';

global.XMLHttpRequest = xhr2;
global.WebSocket = WebSocket;

const wss = new WebSocketServer({ port: 2222 });
wss.on('connection', function connection(ws) {
  ws.on('error', console.error);
  ws.send('{"activities": []}');
});

nock('https://directline.botframework.com')
.persist()
.post(uri => uri.startsWith('/v3/directline/conversations'))
.reply(
  200,
  JSON.stringify({
    conversationId: '123',
    token: '456',
    streamUrl: 'ws://localhost:2222'
  })
)
.post(uri => uri.includes('/refresh'))
.reply(403);

const directLine = new DirectLine({ token: '456' });
directLine.activity$
.subscribe(
  (activity) => console.log('Activity received: ', activity),
  (err) => console.error('**1 Error: ', err)
);
directLine.connectionStatus$
.subscribe(
  (connectionStatus) => console.log('DirectLine status: ' + ConnectionStatus[connectionStatus]),
  (err) => console.error('**2 Error: ', err)
);

process.on('uncaughtException', (err) => {
  console.error('**3 Error: ', err);
  process.exit(1);
});

If you don't want to wait 15 minutes, reduce the hardcoded value of lifetimeRefreshToken :)

Output:

DirectLine status: Connecting
DirectLine status: Online
DirectLine status: ExpiredToken
DirectLine status: ExpiredToken
**3 Error:  [AjaxError: ajax error 403] {
  xhr: <ref *1> XMLHttpRequest {
    onloadstart: null,
    onprogress: null,
    onabort: null,
    onerror: null,
    onload: null,
    ontimeout: [Function: xhrTimeout] {
      request: [Object],
      subscriber: [AjaxSubscriber],
      progressSubscriber: undefined
    },
    onloadend: null,
    _listeners: {},
    onreadystatechange: [Function: xhrReadyStateChange] {
      subscriber: [AjaxSubscriber],
      progressSubscriber: undefined,
      request: [Object]
    },
    _anonymous: undefined,
    readyState: 4,
    response: null,
    responseText: null,
    responseType: 'json',
    responseURL: 'https://directline.botframework.com/v3/directline/tokens/refresh',
    status: 403,
    statusText: 'Forbidden',
...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.