From a9f05fffdffceb76355b4daf5fea6c83563ccedd Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Mon, 3 Jul 2023 13:28:52 +0200
Subject: [PATCH] Fix Error handling in corpus analysis app
---
app/corpora/cqi_over_sio/cqi.py | 23 +++++++------------
app/corpora/cqi_over_sio/extensions.py | 14 ++++++-----
.../js/CorpusAnalysis/CorpusAnalysisApp.js | 7 +++---
.../CorpusAnalysisConcordance.js | 15 ++++++------
.../js/CorpusAnalysis/CorpusAnalysisReader.js | 7 +++---
app/static/js/cqi/api/client.js | 8 +++----
6 files changed, 32 insertions(+), 42 deletions(-)
diff --git a/app/corpora/cqi_over_sio/cqi.py b/app/corpora/cqi_over_sio/cqi.py
index 62a7f63b..2e483b21 100644
--- a/app/corpora/cqi_over_sio/cqi.py
+++ b/app/corpora/cqi_over_sio/cqi.py
@@ -62,13 +62,7 @@ CQI_FUNCTION_NAMES: List[str] = [
@socketio.on('cqi', namespace=ns)
@socketio_login_required
-def cqi_over_sio(fn_data):
- try:
- fn_name: str = fn_data['fn_name']
- except KeyError:
- return {'code': 400, 'msg': 'Bad Request'}
- fn_name: str = fn_data['fn_name']
- fn_args: Dict = fn_data.get('fn_args', {})
+def cqi_over_sio(fn_name: str, fn_args: Dict = {}):
try:
cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_client_lock: Lock = session['cqi_over_sio']['cqi_client_lock']
@@ -77,7 +71,6 @@ def cqi_over_sio(fn_data):
if fn_name in CQI_FUNCTION_NAMES:
fn: Callable = getattr(cqi_client.api, fn_name)
elif fn_name in CQI_EXTENSION_FUNCTION_NAMES:
- fn_args['cqi_client'] = cqi_client
fn: Callable = getattr(extensions_module, fn_name)
else:
return {'code': 400, 'msg': 'Bad Request'}
@@ -92,14 +85,14 @@ def cqi_over_sio(fn_data):
return {'code': 400, 'msg': 'Bad Request'}
cqi_client_lock.acquire()
try:
- return_value = fn(**fn_args)
+ fn_return_value = fn(**fn_args)
except BrokenPipeError:
- return_value = {
+ fn_return_value = {
'code': 500,
'msg': 'Internal Server Error'
}
except CQiException as e:
- return_value = {
+ return {
'code': 502,
'msg': 'Bad Gateway',
'payload': {
@@ -110,11 +103,11 @@ def cqi_over_sio(fn_data):
}
finally:
cqi_client_lock.release()
- if isinstance(return_value, CQiStatus):
+ if isinstance(fn_return_value, CQiStatus):
payload = {
- 'code': return_value.code,
- 'msg': return_value.__class__.__name__
+ 'code': fn_return_value.code,
+ 'msg': fn_return_value.__class__.__name__
}
else:
- payload = return_value
+ payload = fn_return_value
return {'code': 200, 'msg': 'OK', 'payload': payload}
diff --git a/app/corpora/cqi_over_sio/extensions.py b/app/corpora/cqi_over_sio/extensions.py
index 33e00bc0..d92b6d5e 100644
--- a/app/corpora/cqi_over_sio/extensions.py
+++ b/app/corpora/cqi_over_sio/extensions.py
@@ -22,20 +22,22 @@ CQI_EXTENSION_FUNCTION_NAMES: List[str] = [
]
-def ext_corpus_update_db(cqi_client: CQiClient, corpus: str):
+def ext_corpus_update_db(corpus: str):
db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus)
db_corpus.num_tokens = cqi_corpus.size
db.session.commit()
return StatusOk()
-def ext_corpus_static_data(cqi_client: CQiClient, corpus: str) -> Dict:
+def ext_corpus_static_data(corpus: str) -> Dict:
db_corpus = Corpus.query.get(session['cqi_over_sio']['corpus_id'])
static_corpus_data_file = os.path.join(db_corpus.path, 'cwb', 'static.json')
if os.path.exists(static_corpus_data_file):
with open(static_corpus_data_file, 'r') as f:
return json.load(f)
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus)
##########################################################################
# A faster way to get cpos boundaries for smaller s_attrs #
@@ -137,11 +139,11 @@ def ext_corpus_static_data(cqi_client: CQiClient, corpus: str) -> Dict:
def ext_corpus_paginate_corpus(
- cqi_client: CQiClient,
corpus: str,
page: int = 1,
per_page: int = 20
) -> Dict:
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus)
# Sanity checks
if (
@@ -182,13 +184,13 @@ def ext_corpus_paginate_corpus(
def ext_cqp_paginate_subcorpus(
- cqi_client: CQiClient,
subcorpus: str,
context: int = 50,
page: int = 1,
per_page: int = 20
) -> Dict:
corpus_name, subcorpus_name = subcorpus.split(':', 1)
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus_name)
cqi_subcorpus = cqi_corpus.subcorpora.get(subcorpus_name)
# Sanity checks
@@ -230,12 +232,12 @@ def ext_cqp_paginate_subcorpus(
def ext_cqp_partial_export_subcorpus(
- cqi_client: CQiClient,
subcorpus: str,
match_id_list: list,
context: int = 50
) -> Dict:
corpus_name, subcorpus_name = subcorpus.split(':', 1)
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus_name)
cqi_subcorpus = cqi_corpus.subcorpora.get(subcorpus_name)
cqi_subcorpus_partial_export = partial_export_subcorpus(cqi_subcorpus, match_id_list, context=context)
@@ -243,11 +245,11 @@ def ext_cqp_partial_export_subcorpus(
def ext_cqp_export_subcorpus(
- cqi_client: CQiClient,
subcorpus: str,
context: int = 50
) -> Dict:
corpus_name, subcorpus_name = subcorpus.split(':', 1)
+ cqi_client: CQiClient = session['cqi_over_sio']['cqi_client']
cqi_corpus = cqi_client.corpora.get(corpus_name)
cqi_subcorpus = cqi_corpus.subcorpora.get(subcorpus_name)
cqi_subcorpus_export = export_subcorpus(cqi_subcorpus, context=context)
diff --git a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js
index c3adcc7f..8af82e8d 100644
--- a/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js
+++ b/app/static/js/CorpusAnalysis/CorpusAnalysisApp.js
@@ -100,14 +100,13 @@ class CorpusAnalysisApp {
this.elements.m.initModal.close();
},
(cqiError) => {
+ let errorString = `${cqiError.code}: ${cqiError.constructor.name}`;
let errorsElement = this.elements.initModal.querySelector('.errors');
let progressElement = this.elements.initModal.querySelector('.progress');
- errorsElement.innerText = JSON.stringify(cqiError);
+ errorsElement.innerText = errorString;
errorsElement.classList.remove('hide');
+ app.flash(errorString, 'error');
progressElement.classList.add('hide');
- if ('payload' in cqiError && 'code' in cqiError.payload && 'msg' in cqiError.payload) {
- app.flash(`${cqiError.payload.code}: ${cqiError.payload.msg}`, 'error');
- }
}
);
diff --git a/app/static/js/CorpusAnalysis/CorpusAnalysisConcordance.js b/app/static/js/CorpusAnalysis/CorpusAnalysisConcordance.js
index dc1ee3a2..e6c00e73 100644
--- a/app/static/js/CorpusAnalysis/CorpusAnalysisConcordance.js
+++ b/app/static/js/CorpusAnalysis/CorpusAnalysisConcordance.js
@@ -68,13 +68,11 @@ class CorpusAnalysisConcordance {
this.elements.progress.classList.add('hide');
this.app.enableActionElements();
},
- (cqiStatus) => {
- // TODDO: CHECK THIS!
- this.elements.error.innerText = JSON.stringify(cqiStatus);
+ (cqiError) => {
+ let errorString = `${cqiError.code}: ${cqiError.constructor.name}`;
+ this.elements.error.innerText = errorString;
this.elements.error.classList.remove('hide');
- if ('payload' in cqiStatus && 'code' in cqiStatus.payload && 'msg' in cqiStatus.payload) {
- app.flash(`${cqiStatus.payload.code}: ${cqiStatus.payload.msg}`, 'error');
- }
+ app.flash(errorString, 'error');
this.elements.progress.classList.add('hide');
this.app.enableActionElements();
}
@@ -313,8 +311,9 @@ class CorpusAnalysisConcordance {
this.clearSubcorpusPagination();
}
},
- (cQiError) => {
- app.flash(`${cQiError.payload.code}: ${cQiError.payload.msg}`, 'error');
+ (cqiError) => {
+ let errorString = `${cqiError.code}: ${cqiError.constructor.name}`;
+ app.flash(errorString, 'error');
}
);
});
diff --git a/app/static/js/CorpusAnalysis/CorpusAnalysisReader.js b/app/static/js/CorpusAnalysis/CorpusAnalysisReader.js
index 33dd6f47..d6c99926 100644
--- a/app/static/js/CorpusAnalysis/CorpusAnalysisReader.js
+++ b/app/static/js/CorpusAnalysis/CorpusAnalysisReader.js
@@ -45,11 +45,10 @@ class CorpusAnalysisReader {
this.app.enableActionElements();
},
(cqiError) => {
- this.elements.error.innerText = JSON.stringify(error);
+ let errorString = `${cqiError.code}: ${cqiError.constructor.name}`;
+ this.elements.error.innerText = errorString;
this.elements.error.classList.remove('hide');
- if ('payload' in error && 'code' in error.payload && 'msg' in error.payload) {
- app.flash(`${error.payload.code}: ${error.payload.msg}`, 'error');
- }
+ app.flash(errorString, 'error');
this.elements.progress.classList.add('hide');
this.app.enableActionElements();
}
diff --git a/app/static/js/cqi/api/client.js b/app/static/js/cqi/api/client.js
index c6c7e5b9..daf2a401 100644
--- a/app/static/js/cqi/api/client.js
+++ b/app/static/js/cqi/api/client.js
@@ -30,14 +30,12 @@ cqi.api.APIClient = class APIClient {
// if (timeoutError) {
// reject(timeoutError);
// }
- this.socket.emit('cqi', {fn_name: fn_name, fn_args: fn_args}, (response) => {
+ this.socket.emit('cqi', fn_name, fn_args, (response) => {
if (response.code === 200) {
resolve(response.payload);
- }
- if (response.code === 500) {
+ } else if (response.code === 500) {
reject(new Error(`[${response.code}] ${response.msg}`));
- }
- if (response.code === 502) {
+ } else if (response.code === 502) {
reject(new cqi.errors.lookup[response.payload.code]());
}
});