From 2c66a8a850ca1a25e6b00f6c42d423328621ede7 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 1 Jun 2022 11:39:35 +0200 Subject: [PATCH] Streamline error handling in qtloader.js Added new function handleError which does the usual work whenever there there is an error, including logging the error to console. Also make the app exit when the emscripten module fails to load. Additionally, make sure we correctly report it as crash if the module fails to load. Change-Id: I9d723373a34ccbb146959a2207ebded8bcbd4f18 Reviewed-by: Lorn Potter --- src/plugins/platforms/wasm/qtloader.js | 31 +++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/plugins/platforms/wasm/qtloader.js b/src/plugins/platforms/wasm/qtloader.js index 33280237abd..f51a574078f 100644 --- a/src/plugins/platforms/wasm/qtloader.js +++ b/src/plugins/platforms/wasm/qtloader.js @@ -61,6 +61,8 @@ // Optional exited element constructor function. // showError : function(crashed, exitCode, containerElement) // Optional error element constructor function. +// statusChanged : function(newStatus) +// Optional callback called when the status of the app has changed // // path : // Prefix path for wasm file, realative to the loading HMTL file. @@ -232,13 +234,19 @@ function QtLoader(config) self.restartCount = 0; + function handleError(error) { + self.error = error; + setStatus("Error"); + console.error(error); + } + function fetchResource(filePath) { var fullPath = config.path + filePath; return fetch(fullPath).then(function(response) { if (!response.ok) { - self.error = response.status + " " + response.statusText + " " + response.url; - setStatus("Error"); - return Promise.reject(self.error) + let err = response.status + " " + response.statusText + " " + response.url; + handleError(err); + return Promise.reject(err) } else { return response; } @@ -287,13 +295,11 @@ function QtLoader(config) // Check for Wasm & WebGL support; set error and return before downloading resources if missing if (!webAssemblySupported()) { - self.error = "Error: WebAssembly is not supported" - setStatus("Error"); + handleError("Error: WebAssembly is not supported"); return; } if (!webGLSupported()) { - self.error = "Error: WebGL is not supported" - setStatus("Error"); + handleError("Error: WebGL is not supported"); return; } @@ -319,8 +325,9 @@ function QtLoader(config) Promise.all([emscriptenModuleSourcePromise, wasmModulePromise]).then(function(){ completeLoadEmscriptenModule(applicationName, emscriptenModuleSource, wasmModule); }).catch(function(error) { - self.error = error; - setStatus("Error"); + handleError(error); + // An error here is fatal, abort + self.moduleConfig.onAbort(error) }); } @@ -333,8 +340,7 @@ function QtLoader(config) WebAssembly.instantiate(wasmModule, imports).then(function(instance) { successCallback(instance, wasmModule); }, function(error) { - self.error = error; - setStatus("Error"); + handleError(error) }); return {}; }; @@ -418,8 +424,7 @@ function QtLoader(config) // Restart by readling the emscripten app module. ++self.restartCount; if (self.restartCount > config.restartLimit) { - self.error = "Error: This application has crashed too many times and has been disabled. Reload the page to try again." - setStatus("Error"); + handleError("Error: This application has crashed too many times and has been disabled. Reload the page to try again."); return; } loadEmscriptenModule(applicationName);