Skip to content

Commit

Permalink
Merge pull request #469 from France-ioi/fix-error-handling
Browse files Browse the repository at this point in the history
When an error occurs in saga, make the error appear and then restart …
  • Loading branch information
SebastienTainon authored Apr 12, 2024
2 parents df3ecde + d8bcf76 commit b4d76ac
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
30 changes: 25 additions & 5 deletions frontend/linker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {default as createSagaMiddleware, Saga} from 'redux-saga';
import {default as createSagaMiddleware} from 'redux-saga';
import {all, call} from 'typed-redux-saga';
import produce from "immer";
import {AppStore} from "./store";
Expand All @@ -11,8 +11,10 @@ import platformSlice from "./task/platform/platform_slice";
import analysisSlice from "./stepper/analysis/analysis_slice";
import modalSlice from "./common/modal_slice";
import submissionSlice from "./submission/submission_slice";
import {App, CodecastEnvironmentMonitoring} from './app_types';
import {App, Codecast, CodecastEnvironmentMonitoring} from './app_types';
import buffersSlice from './buffers/buffers_slice';
import {stepperExecutionError, stepperDisplayError} from './stepper/actionTypes';
import {LibraryTestResult} from './task/libs/library_test_result';

export interface Linker {
scope: App,
Expand Down Expand Up @@ -177,15 +179,33 @@ export function link(rootBuilder, globalScope: App): Linker {
},
};

let lastSagaTermination = new Date().getTime();

// Compose the enhancers.
const sagaMiddleware = createSagaMiddleware({
sagaMonitor,
onError: (error, {sagaStack}) => {
console.error(error);
console.error(sagaStack);
setImmediate(() => {
throw error;
});

const mainStore = Codecast.environments['main'].store;
if ('background' === globalScope.environment) {
mainStore.dispatch(stepperExecutionError(LibraryTestResult.fromString(error.message), false));
} else {
mainStore.dispatch(stepperDisplayError(error.message));
// When the error bubbles up to this onError listener, redux-saga terminates the saga. So we have to restart them
}

let currentDate = new Date().getTime();
if (currentDate - lastSagaTermination > 1000) {
setTimeout(() => {
Codecast.restartSagas();
});
}

lastSagaTermination = currentDate;

throw error;
}
});

Expand Down
3 changes: 2 additions & 1 deletion frontend/stepper/actionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ export const stepperExecutionSuccess = (testResult: LibraryTestResult) => ({
},
});

export const stepperExecutionError = (testResult: LibraryTestResult) => ({
export const stepperExecutionError = (testResult: LibraryTestResult, display = true) => ({
type: ActionTypes.StepperExecutionError,
payload: {
testResult,
display,
},
});

Expand Down
4 changes: 3 additions & 1 deletion frontend/stepper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,9 @@ function* stepperSaga(app: App) {
// @ts-ignore
yield* takeEvery([StepperActionTypes.StepperExecutionError, StepperActionTypes.CompileFailed], function*({payload}) {
log.getLogger('stepper').debug('receive an error, display it');
yield* put(stepperDisplayError(payload.testResult));
if (false !== payload?.display) {
yield* put(stepperDisplayError(payload.testResult));
}
});
}

Expand Down
8 changes: 8 additions & 0 deletions frontend/task/documentation/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ export default function (bundle: Bundle) {
}
});


});

bundle.defer(function (app: App) {
if ('main' !== app.environment) {
return;
}

addAutoRecordingBehaviour(app, {
actions: [
documentationUseCodeExample,
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = (env, argv) => {
fs: false,
crypto: require.resolve('crypto-browserify'),
util: require.resolve('util'),
vm: false,
},
alias: {
lamejs: 'lamejs/src/js/',
Expand Down

0 comments on commit b4d76ac

Please sign in to comment.