Skip to content

Commit

Permalink
8693ca0ja: better error handling for missing cuis_file json files
Browse files Browse the repository at this point in the history
  • Loading branch information
tomolopolis committed Dec 21, 2023
1 parent 79dc51a commit b9540d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 10 additions & 1 deletion webapp/api/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging

from django.contrib.auth.models import User
from rest_framework.fields import FileField
Expand All @@ -8,6 +9,8 @@
from rest_framework import serializers


logger = logging.getLogger(__name__)

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
Expand Down Expand Up @@ -69,7 +72,13 @@ class Meta:

def to_representation(self, instance):
data = super(ProjectAnnotateEntitiesSerializer, self).to_representation(instance)
cuis_from_file = json.load(open(instance.cuis_file.path)) if instance.cuis_file else []
try:
cuis_from_file = json.load(open(instance.cuis_file.path)) if instance.cuis_file else []
except FileNotFoundError:
logger.warning('cuis file %s cannot be found. Project: %s will fail to load. File '
'needs to be cleared and re-uploaded to load project', instance.cuis_file,
instance.name)
cuis_from_file = []
cui_list = [s.strip() for s in data['cuis'].split(',')] + cuis_from_file if len(data['cuis']) > 0 else cuis_from_file
data['cuis'] = ','.join(cui_list)
return data
Expand Down
9 changes: 7 additions & 2 deletions webapp/api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ def prepare_documents(request):
cuis = set([str(cui).strip() for cui in project.cuis.split(",")])
if project.cuis_file is not None and project.cuis_file:
# Add cuis from json file if it exists
cuis.update(json.load(open(project.cuis_file.path)))

try:
cuis.update(json.load(open(project.cuis_file.path)))
except FileNotFoundError:
return Response({'message': 'Missing CUI filter file',
'description': 'Missing CUI filter file, %s, cannot be found on the filesystem, '
'but is still set on the project. To fix remove and reset the '
'cui filter file' % project.cuis_file}, status=500)
try:
for d_id in d_ids:
document = Document.objects.get(id=d_id)
Expand Down

0 comments on commit b9540d4

Please sign in to comment.