From 7a925b6a194635e2835f91847e72f047a406c299 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Tue, 18 Jul 2023 17:18:04 +0200 Subject: [PATCH 1/2] Better error handling in CorpusAnalysisApp --- .../js/CorpusAnalysis/CorpusAnalysisApp.js | 33 ++++++++++--------- app/static/js/cqi/api/client.js | 6 +++- app/templates/corpora/analysis.html.j2 | 3 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js index 2d69fed7..d6274f32 100644 --- a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js +++ b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js @@ -25,22 +25,21 @@ class CorpusAnalysisApp { async init() { this.disableActionElements(); this.elements.m.initModal.open(); - - const statusTextElement = this.elements.initModal.querySelector('.status-text'); - - // Setup CQi over SocketIO connection and gather data from the CQPServer + try { + // Setup CQi over SocketIO connection and gather data from the CQPServer + const statusTextElement = this.elements.initModal.querySelector('.status-text'); statusTextElement.innerText = 'Creating CQi over SocketIO client...'; const cqiClient = new cqi.CQiClient('/cqi_over_sio'); statusTextElement.innerText += ' Done'; - statusTextElement.innerHTML += '
Waiting for the CQP server...'; + statusTextElement.innerHTML = 'Waiting for the CQP server...'; const response = await cqiClient.api.socket.emitWithAck('init', this.corpusId); if (response.code !== 200) {throw new Error();} statusTextElement.innerText += ' Done'; - statusTextElement.innerHTML += '
Connecting to the CQP server...'; + statusTextElement.innerHTML = 'Connecting to the CQP server...'; await cqiClient.connect('anonymous', ''); statusTextElement.innerText += ' Done'; - statusTextElement.innerHTML += '
Building and receiving corpus data cache from the server (This may take a while)...'; + statusTextElement.innerHTML = 'Building and receiving corpus data cache from the server (This may take a while)...'; const cqiCorpus = await cqiClient.corpora.get(`NOPAQUE-${this.corpusId.toUpperCase()}`); statusTextElement.innerText += ' Done'; // TODO: Don't do this hgere @@ -48,11 +47,21 @@ class CorpusAnalysisApp { this.data.cqiClient = cqiClient; this.data.cqiCorpus = cqiCorpus; this.data.corpus = {o: cqiCorpus}; // legacy + // Initialize extensions + for (const extension of Object.values(this.extensions)) { + statusTextElement.innerHTML = `Initializing ${extension.name} extension...`; + await extension.init(); + statusTextElement.innerText += ' Done' + } } catch (error) { let errorString = ''; - if ('code' in error) {errorString += `[${error.code}] `;} + if ('code' in error && error.code !== undefined && error.code !== null) { + errorString += `[${error.code}] `; + } errorString += `${error.constructor.name}`; - if ('description' in error) {errorString += `: ${error.description}`;} + if ('description' in error && error.description !== undefined && error.description !== null) { + errorString += `: ${error.description}`; + } const errorsElement = this.elements.initModal.querySelector('.errors'); const progressElement = this.elements.initModal.querySelector('.progress'); errorsElement.innerText = errorString; @@ -61,12 +70,6 @@ class CorpusAnalysisApp { return; } - // Initialize extensions - for (const extension of Object.values(this.extensions)) { - statusTextElement.innerHTML += `
Initializing ${extension.name} extension...`; - await extension.init(); - statusTextElement.innerText += ' Done' - } for (const extensionSelectorElement of this.elements.extensionCards.querySelectorAll('.extension-selector')) { extensionSelectorElement.addEventListener('click', () => { this.elements.m.extensionTabs.select(extensionSelectorElement.dataset.target); diff --git a/app/static/js/cqi/api/client.js b/app/static/js/cqi/api/client.js index 82f8176f..14bf3690 100644 --- a/app/static/js/cqi/api/client.js +++ b/app/static/js/cqi/api/client.js @@ -30,7 +30,11 @@ cqi.api.APIClient = class APIClient { } else if (response.code === 500) { throw new Error(`[${response.code}] ${response.msg}`); } else if (response.code === 502) { - throw new cqi.errors.lookup[response.payload.code](); + if (response.payload.code in cqi.errors.lookup) { + throw new cqi.errors.lookup[response.payload.code](); + } else { + throw new cqi.errors.CQiError(); + } } } diff --git a/app/templates/corpora/analysis.html.j2 b/app/templates/corpora/analysis.html.j2 index 9d4c27aa..2938f886 100644 --- a/app/templates/corpora/analysis.html.j2 +++ b/app/templates/corpora/analysis.html.j2 @@ -48,8 +48,9 @@ {{ super() }}