Skip to content

Commit

Permalink
Migrate disconnectionErrorCodes into constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Aug 21, 2024
1 parent bbbd09e commit 8311258
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
2 changes: 2 additions & 0 deletions kolibri/core/assets/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ export const Presets = Object.freeze({
// This should be kept in sync with the value in
// kolibri/core/exams/constants.py
export const MAX_QUESTIONS_PER_QUIZ_SECTION = 25;

export const DisconnectionErrorCodes = [0, 502, 504, 511];
4 changes: 2 additions & 2 deletions kolibri/core/assets/src/core-app/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import qs from 'qs';
import heartbeat from 'kolibri.heartbeat';
import logger from 'kolibri.lib.logging';
import store from 'kolibri.coreVue.vuex.store';
import errorCodes from '../disconnectionErrorCodes';
import { DisconnectionErrorCodes } from '../constants';
import clientFactory from './baseClient';

export const logging = logger.getLogger(__filename);
Expand Down Expand Up @@ -53,7 +53,7 @@ baseClient.interceptors.response.use(
}
// On every error, check to see if the status code is one of our designated
// disconnection status codes.
if (errorCodes.includes(error.response.status)) {
if (DisconnectionErrorCodes.includes(error.response.status)) {
// If so, set our heartbeat module to start monitoring the disconnection state
heartbeat.monitorDisconnect(error.response.status);
}
Expand Down
1 change: 0 additions & 1 deletion kolibri/core/assets/src/disconnectionErrorCodes.js

This file was deleted.

9 changes: 4 additions & 5 deletions kolibri/core/assets/src/heartbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import redirectBrowser from 'kolibri.utils.redirectBrowser';
import Lockr from 'lockr';
import urls from 'kolibri.urls';
import clientFactory from './core-app/baseClient';
import { SIGNED_OUT_DUE_TO_INACTIVITY } from './constants';
import errorCodes from './disconnectionErrorCodes';
import { DisconnectionErrorCodes, SIGNED_OUT_DUE_TO_INACTIVITY } from './constants';
import {
createTryingToReconnectSnackbar,
createDisconnectedSnackbar,
Expand Down Expand Up @@ -58,7 +57,7 @@ export class HeartBeat {
function (response) {
// If the response does not have one of the disconnect error codes
// then we have reconnected.
if (!store.getters.connected && !errorCodes.includes(response.status)) {
if (!store.getters.connected && !DisconnectionErrorCodes.includes(response.status)) {
// Not one of our 'disconnected' status codes, so we are connected again
// Set connected and return the response here to prevent any further processing.
heartbeat._setConnected();
Expand All @@ -70,7 +69,7 @@ export class HeartBeat {
if (!connected) {
// If the response does not have one of the disconnect error codes
// then we have reconnected.
if (!errorCodes.includes(error.response.status)) {
if (!DisconnectionErrorCodes.includes(error.response.status)) {
// Not one of our 'disconnected' status codes, so we are connected again
// Set connected and return the response here to prevent any further processing.
heartbeat._setConnected();
Expand Down Expand Up @@ -209,7 +208,7 @@ export class HeartBeat {
.catch(error => {
// An error occurred.
logging.error('Session polling failed, with error: ', error);
if (errorCodes.includes(error.response.status)) {
if (DisconnectionErrorCodes.includes(error.response.status)) {
// We had an error that indicates that we are disconnected, so start to monitor
// the disconnection.
return this.monitorDisconnect(error.response.status);
Expand Down
10 changes: 7 additions & 3 deletions kolibri/core/assets/src/state/modules/core/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ import CatchErrors from 'kolibri.utils.CatchErrors';
import Vue from 'kolibri.lib.vue';
import Lockr from 'lockr';
import { baseSessionState } from '../session';
import { LoginErrors, ERROR_CONSTANTS, UPDATE_MODAL_DISMISSED } from '../../../constants';
import {
DisconnectionErrorCodes,
LoginErrors,
ERROR_CONSTANTS,
UPDATE_MODAL_DISMISSED,
} from '../../../constants';
import { browser, os } from '../../../utils/browserInfo';
import errorCodes from './../../../disconnectionErrorCodes.js';

const logging = logger.getLogger(__filename);

Expand Down Expand Up @@ -76,7 +80,7 @@ export function handleApiError(store, { error, reloadOnReconnect = false } = {})
if (typeof error === 'object' && !(error instanceof Error)) {
errorString = JSON.stringify(error, null, 2);
} else if (error.response) {
if (errorCodes.includes(error.response.status)) {
if (DisconnectionErrorCodes.includes(error.response.status)) {
// Do not log errors for disconnections, as it disrupts the user experience
// and should already be being handled by our disconnection overlay.
store.commit('CORE_SET_RELOAD_ON_RECONNECT', reloadOnReconnect);
Expand Down
58 changes: 27 additions & 31 deletions kolibri/core/assets/test/heartbeat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import coreStore from 'kolibri.coreVue.vuex.store';
import redirectBrowser from 'kolibri.utils.redirectBrowser';
import * as serverClock from 'kolibri.utils.serverClock';
import { HeartBeat } from '../src/heartbeat.js';
import disconnectionErrorCodes from '../src/disconnectionErrorCodes';
import { DisconnectionErrorCodes } from '../src/constants';
import { trs } from '../src/disconnection';
import coreModule from '../src/state/modules/core';
import { stubWindowLocation } from 'testUtils'; // eslint-disable-line
Expand Down Expand Up @@ -250,20 +250,18 @@ describe('HeartBeat', function () {
// Rather it is the status code that our request client library returns
// when the connection is refused by the host, or is otherwise unable to connect.
// What happens for a zero code is tested later in this file.
disconnectionErrorCodes
.filter(code => code !== 0)
.forEach(errorCode => {
it('should call monitorDisconnect if it receives error code ' + errorCode, function () {
const monitorStub = jest.spyOn(heartBeat, 'monitorDisconnect');
mock.put(/.*/, {
status: errorCode,
headers: { 'Content-Type': 'application/json' },
});
return heartBeat._checkSession().finally(() => {
expect(monitorStub).toHaveBeenCalledTimes(1);
});
DisconnectionErrorCodes.filter(code => code !== 0).forEach(errorCode => {
it('should call monitorDisconnect if it receives error code ' + errorCode, function () {
const monitorStub = jest.spyOn(heartBeat, 'monitorDisconnect');
mock.put(/.*/, {
status: errorCode,
headers: { 'Content-Type': 'application/json' },
});
return heartBeat._checkSession().finally(() => {
expect(monitorStub).toHaveBeenCalledTimes(1);
});
});
});
});
describe('when not connected', function () {
beforeEach(function () {
Expand All @@ -274,26 +272,24 @@ describe('HeartBeat', function () {
expect(coreStore.getters.snackbarIsVisible).toEqual(true);
expect(coreStore.getters.snackbarOptions.text).toEqual(trs.$tr('tryingToReconnect'));
});
disconnectionErrorCodes
.filter(code => code !== 0)
.forEach(errorCode => {
it('should set snackbar to disconnected for error code ' + errorCode, function () {
jest.spyOn(heartBeat, 'monitorDisconnect');
mock.put(/.*/, {
status: errorCode,
headers: { 'Content-Type': 'application/json' },
});
heartBeat._wait = jest.fn();
return heartBeat._checkSession().finally(() => {
expect(coreStore.getters.snackbarIsVisible).toEqual(true);
expect(
coreStore.getters.snackbarOptions.text.startsWith(
'Disconnected from server. Will try to reconnect in',
),
).toEqual(true);
});
DisconnectionErrorCodes.filter(code => code !== 0).forEach(errorCode => {
it('should set snackbar to disconnected for error code ' + errorCode, function () {
jest.spyOn(heartBeat, 'monitorDisconnect');
mock.put(/.*/, {
status: errorCode,
headers: { 'Content-Type': 'application/json' },
});
heartBeat._wait = jest.fn();
return heartBeat._checkSession().finally(() => {
expect(coreStore.getters.snackbarIsVisible).toEqual(true);
expect(
coreStore.getters.snackbarOptions.text.startsWith(
'Disconnected from server. Will try to reconnect in',
),
).toEqual(true);
});
});
});
it('should set snackbar to disconnected for error code 0', function () {
jest.spyOn(heartBeat, 'monitorDisconnect');
mock.put(/.*/, () => Promise.reject(new Error()));
Expand Down

0 comments on commit 8311258

Please sign in to comment.