Fix Error handling in corpus analysis app

This commit is contained in:
Patrick Jentsch 2023-07-03 13:28:52 +02:00
parent 7936ac270b
commit a9f05fffdf
6 changed files with 32 additions and 42 deletions

View File

@ -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}

View File

@ -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)

View File

@ -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');
}
}
);

View File

@ -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');
}
);
});

View File

@ -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();
}

View File

@ -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]());
}
});