more exception handling

This commit is contained in:
Patrick Jentsch 2021-05-08 16:47:41 +02:00
parent 966cdc824a
commit bc87bf67bf

View File

@ -70,14 +70,10 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
connect_status = client.connect() connect_status = client.connect()
payload = {'code': connect_status, 'msg': cqi.api.specification.lookup[connect_status]} # noqa payload = {'code': connect_status, 'msg': cqi.api.specification.lookup[connect_status]} # noqa
except cqi.errors.CQiException as e: except cqi.errors.CQiException as e:
payload = {'code': e.code, 'desc': e.description, 'msg': e.name} handle_cqi_exception('corpus_analysis_init', e, session_id)
response = {'code': 500, 'desc': None,
'msg': 'Internal Server Error', 'payload': payload}
socketio.emit('corpus_analysis_init', response, room=session_id)
return return
except gaierror: except gaierror:
response = {'code': 500, 'desc': None, response = {'code': 500, 'desc': None, 'msg': 'Internal Server Error'} # noqa
'msg': 'Internal Server Error'}
socketio.emit('corpus_analysis_init', response, room=session_id) socketio.emit('corpus_analysis_init', response, room=session_id)
return return
corpus_analysis_clients[session_id] = client corpus_analysis_clients[session_id] = client
@ -127,7 +123,7 @@ def corpus_analysis_get_meta_data(corpus_id):
if client is None: if client is None:
response = {'code': 424, 'desc': 'No client found for this session', response = {'code': 424, 'desc': 'No client found for this session',
'msg': 'Failed Dependency'} 'msg': 'Failed Dependency'}
socketio.emit('corpus_analysis_query', response, room=request.sid) socketio.emit('corpus_analysis_meta_data', response, room=request.sid)
return return
# check if client is busy or not # check if client is busy or not
if client.status == 'running': if client.status == 'running':
@ -197,10 +193,8 @@ def corpus_analysis_query(query):
query_status = corpus.query(query) query_status = corpus.query(query)
results = corpus.subcorpora.get('Results') results = corpus.subcorpora.get('Results')
except cqi.errors.CQiException as e: except cqi.errors.CQiException as e:
payload = {'code': e.code, 'desc': e.description, 'msg': e.name} client.status = 'ready'
response = {'code': 500, 'desc': None, 'msg': 'Internal Server Error', handle_cqi_exception('corpus_analysis_query', e, request.sid)
'payload': payload}
socketio.emit('corpus_analysis_query', response, room=request.sid)
return return
payload = {'status': query_status, payload = {'status': query_status,
'msg': cqi.api.specification.lookup[query_status], 'msg': cqi.api.specification.lookup[query_status],
@ -214,17 +208,19 @@ def corpus_analysis_query(query):
while chunk_start <= results.attrs['size']: while chunk_start <= results.attrs['size']:
if client.status == 'abort': if client.status == 'abort':
break break
chunk = results.export(context=context, cutoff=chunk_size, try:
offset=chunk_start) chunk = results.export(context=context, cutoff=chunk_size, offset=chunk_start) # noqa
except cqi.errors.CQiException as e:
handle_cqi_exception('corpus_analysis_query', e, request.sid)
break
if (results.attrs['size'] == 0): if (results.attrs['size'] == 0):
progress = 100 progress = 100
else: else:
progress = ((chunk_start + chunk_size) / results.attrs['size']) * 100 # noqa progress = ((chunk_start + chunk_size) / results.attrs['size']) * 100 # noqa
progress = min(100, int(math.ceil(progress))) progress = min(100, int(math.ceil(progress)))
response = {'code': 200, 'desc': None, 'msg': 'OK', payload = {'chunk': chunk, 'progress': progress}
'payload': {'chunk': chunk, 'progress': progress}} response = {'code': 200, 'desc': None, 'msg': 'OK', 'payload': payload}
socketio.emit('corpus_analysis_query_results', response, socketio.emit('corpus_analysis_query_results', response, room=request.sid) # noqa
room=request.sid)
chunk_start += chunk_size chunk_start += chunk_size
client.status = 'ready' client.status = 'ready'
@ -238,11 +234,8 @@ def corpus_analysis_get_match_with_full_context(payload):
last_cpos = payload['last_cpos'] last_cpos = payload['last_cpos']
client = corpus_analysis_clients.get(request.sid) client = corpus_analysis_clients.get(request.sid)
if client is None: if client is None:
response = {'code': 424, response = {'code': 424, 'desc': 'No client found for this session',
'desc': 'No client found for this session', 'msg': 'Failed Dependency'}
'msg': 'Failed Dependency',
'type': type,
'data_indexes': data_indexes}
socketio.emit('corpus_analysis_get_match_with_full_context', response, socketio.emit('corpus_analysis_get_match_with_full_context', response,
room=request.sid) room=request.sid)
return return
@ -254,42 +247,30 @@ def corpus_analysis_get_match_with_full_context(payload):
try: try:
corpus = client.corpora.get('CORPUS') corpus = client.corpora.get('CORPUS')
s = corpus.structural_attributes.get('s') s = corpus.structural_attributes.get('s')
payload = {} except cqi.errors.CQiException as e:
payload['matches'] = [] handle_cqi_exception('corpus_analysis_get_match_with_full_context', e, request.sid) # noqa
payload['cpos_lookup'] = {} return
payload['text_lookup'] = {}
payload['progress'] = 0
i = 0 i = 0
# Send data one match at a time. # Send data one match at a time.
for index, f_cpos, l_cpos in zip(data_indexes, first_cpos, last_cpos): for index, f_cpos, l_cpos in zip(data_indexes, first_cpos, last_cpos):
i += 1 i += 1
tmp_match = s.export(f_cpos, l_cpos, context=10) matches = []
payload['matches'].append(tmp_match['matches'][0]) cpos_lookup = text_lookup = {}
payload['cpos_lookup'].update(tmp_match['cpos_lookup']) try:
payload['text_lookup'].update(tmp_match['text_lookup']) tmp = s.export(f_cpos, l_cpos, context=10)
payload['progress'] = i/len(data_indexes)*100 except cqi.errors.CQiException as e:
response = {'code': 200, handle_cqi_exception('corpus_analysis_get_match_with_full_context', e, request.sid) # noqa
'desc': None, break
'msg': 'OK', matches.append(tmp['matches'][0])
'payload': payload, cpos_lookup.update(tmp['cpos_lookup'])
'type': type, text_lookup.update(tmp['text_lookup'])
'data_indexes': data_indexes} progress = i / len(data_indexes) * 100
payload = {'matches': matches, 'progress': progress,
'cpos_lookup': cpos_lookup, 'text_lookup': text_lookup}
response = {'code': 200, 'desc': None, 'msg': 'OK', 'payload': payload,
'type': type, 'data_indexes': data_indexes}
socketio.emit('corpus_analysis_get_match_with_full_context', socketio.emit('corpus_analysis_get_match_with_full_context',
response, room=request.sid) response, room=request.sid)
payload['matches'] = []
payload['cpos_lookup'] = {}
payload['text_lookup'] = {}
except cqi.errors.CQiException as e:
payload = {'code': e.code, 'desc': e.description, 'msg': e.name}
response = {'code': 500,
'desc': None,
'msg': 'Internal Server Error',
'payload': payload,
'type': type,
'data_indexes': data_indexes}
socketio.emit('corpus_analysis_get_match_with_full_context',
response,
room=request.sid)
client.status = 'ready' client.status = 'ready'
@ -301,7 +282,7 @@ def export_corpus(corpus_id):
response = {'code': 404, 'msg': 'Not found'} response = {'code': 404, 'msg': 'Not found'}
socketio.emit('export_corpus', response, room=request.sid) socketio.emit('export_corpus', response, room=request.sid)
return return
if corpus.status not in ['prepared', 'start analysis', 'stop analysis']: if corpus.status != 'prepared':
response = {'code': 412, 'msg': 'Precondition Failed'} response = {'code': 412, 'msg': 'Precondition Failed'}
socketio.emit('export_corpus', response, room=request.sid) socketio.emit('export_corpus', response, room=request.sid)
return return
@ -315,3 +296,13 @@ def export_corpus(corpus_id):
shutil.make_archive(zip_path, 'zip', corpus.path) shutil.make_archive(zip_path, 'zip', corpus.path)
shutil.move(zip_path + '.zip', corpus.archive_file) shutil.move(zip_path + '.zip', corpus.archive_file)
socketio.emit('export_corpus_' + str(corpus.id), room=request.sid) socketio.emit('export_corpus_' + str(corpus.id), room=request.sid)
def handle_cqi_exception(event, exception, room):
response = {'code': 500,
'desc': None,
'msg': 'Internal Server Error',
'payload': {'code': exception.code,
'desc': exception.description,
'msg': exception.name}}
socketio.emit(event, response, room=room)