Unverified Commit 572c7f02 authored by Denys Mishunov's avatar Denys Mishunov
Browse files

fix(chat): catch streaming error in webide to not crash chat

parent 4a742228
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
import WebSocket from 'ws';
import { createCable } from '@anycable/core';
import { connectToCable } from './action_cable';
import { log } from '../../log';

const cableMock = {
  connect: jest.fn(),
@@ -10,6 +11,12 @@ jest.mock('@anycable/core', () => ({
  createCable: jest.fn().mockImplementation(() => cableMock),
}));

jest.mock('../../log', () => ({
  log: {
    error: jest.fn(),
  },
}));

describe('connectToCable', () => {
  it('creates new anycable cable and connects', async () => {
    const connection = await connectToCable('https://foo.bar');
@@ -38,4 +45,14 @@ describe('connectToCable', () => {
      websocketOptions: additionalOptions,
    });
  });

  it('logs error is connection to cable fails', async () => {
    const err = new Error('Foo Bar');
    cableMock.connect = jest.fn(() => {
      throw err;
    });

    await connectToCable('http://foo.bar:3000');
    expect(log.error).toHaveBeenCalledWith(err);
  });
});
+5 −0
Original line number Diff line number Diff line
import WebSocket from 'isomorphic-ws';
import { createCable } from '@anycable/core';
import { log } from '../../log';

export const connectToCable = async (instanceUrl: string, websocketOptions?: object) => {
  const cableUrl = new URL('/-/cable', instanceUrl);
@@ -10,7 +11,11 @@ export const connectToCable = async (instanceUrl: string, websocketOptions?: obj
    websocketOptions,
  });

  try {
    await cable.connect();
  } catch (e) {
    log.error(e);
  }

  return cable;
};