summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/errors.js
blob: 41eb6902ff654633762b3d0aca91f1c8f4512627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { apply, call, cancelled, put, take } from 'redux-saga/effects';

import { createSubscriptionChannel } from '../utils/websocket';
import { actions } from '../redux/errors';

import { toastr } from 'react-redux-toastr';

export default function* errorHandlingSaga({ socket }) {
  const errorChannel = yield call(createSubscriptionChannel, socket, '/user/queue/errors');
  try {
    while (true) {
      const error = yield take(errorChannel);
      yield* handleOneError(error);
    }
  } finally {
    if (yield cancelled()) {
      console.log('Error management saga cancelled');
      yield apply(errorChannel, errorChannel.close);
    }
  }
}

function* handleOneError(err) {
  console.error('Error received on web socket channel', err);
  const msg = buildMsg(err);
  yield apply(toastr, toastr.error, [msg, { icon: 'error' }]);
  yield put(actions.errorReceived(err));
}

function buildMsg(err) {
  if (err.details.length > 0) {
    return err.details.map(d => d.message).join('\n');
  } else {
    return err.message;
  }
}
bgstack15