From d8bcf763f3e660164699633360afb6aef7577f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Tainon?= Date: Fri, 12 Apr 2024 15:49:35 +0200 Subject: [PATCH] When an error occurs in saga, make the error appear and then restart the sagas so that the interface is not frozen --- frontend/linker.ts | 30 +++++++++++++++++++++++++----- frontend/stepper/actionTypes.ts | 3 ++- frontend/stepper/index.ts | 4 +++- frontend/task/documentation/doc.ts | 8 ++++++++ webpack.config.js | 1 + 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/frontend/linker.ts b/frontend/linker.ts index 3cf99e65..cac76c65 100644 --- a/frontend/linker.ts +++ b/frontend/linker.ts @@ -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"; @@ -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, @@ -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; } }); diff --git a/frontend/stepper/actionTypes.ts b/frontend/stepper/actionTypes.ts index e80f8c19..fdde085b 100644 --- a/frontend/stepper/actionTypes.ts +++ b/frontend/stepper/actionTypes.ts @@ -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, }, }); diff --git a/frontend/stepper/index.ts b/frontend/stepper/index.ts index 2337b69a..97852be1 100644 --- a/frontend/stepper/index.ts +++ b/frontend/stepper/index.ts @@ -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)); + } }); } diff --git a/frontend/task/documentation/doc.ts b/frontend/task/documentation/doc.ts index 62fa13a6..b00a2af8 100644 --- a/frontend/task/documentation/doc.ts +++ b/frontend/task/documentation/doc.ts @@ -395,6 +395,14 @@ export default function (bundle: Bundle) { } }); + + }); + + bundle.defer(function (app: App) { + if ('main' !== app.environment) { + return; + } + addAutoRecordingBehaviour(app, { actions: [ documentationUseCodeExample, diff --git a/webpack.config.js b/webpack.config.js index 297be39d..90b3c638 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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/',