added some exception handling around some file io operations
This commit is contained in:
parent
b18b070622
commit
692a4fe38a
@ -345,10 +345,14 @@ class DocumentViewSet(
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def get_filesize(self, filename):
|
def get_filesize(self, filename):
|
||||||
|
try:
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
return os.stat(filename).st_size
|
return os.stat(filename).st_size
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
except (FileNotFoundError, PermissionError) as e:
|
||||||
|
logger.debug(f'Error: {e}')
|
||||||
|
return None
|
||||||
|
|
||||||
@action(methods=["get"], detail=True)
|
@action(methods=["get"], detail=True)
|
||||||
def metadata(self, request, pk=None):
|
def metadata(self, request, pk=None):
|
||||||
@ -692,8 +696,11 @@ class LogViewSet(ViewSet):
|
|||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
|
try:
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
lines = [line.rstrip() for line in f.readlines()]
|
lines = [line.rstrip() for line in f.readlines()]
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise Http404
|
||||||
|
|
||||||
return Response(lines)
|
return Response(lines)
|
||||||
|
|
||||||
@ -784,9 +791,12 @@ class PostDocumentView(GenericAPIView):
|
|||||||
pathvalidate.sanitize_filename(doc_name),
|
pathvalidate.sanitize_filename(doc_name),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
temp_file_path.write_bytes(doc_data)
|
temp_file_path.write_bytes(doc_data)
|
||||||
|
|
||||||
os.utime(temp_file_path, times=(t, t))
|
os.utime(temp_file_path, times=(t, t))
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Error occurred during file processing: {e}")
|
||||||
|
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
input_doc = ConsumableDocument(
|
input_doc = ConsumableDocument(
|
||||||
source=DocumentSource.ApiUpload,
|
source=DocumentSource.ApiUpload,
|
||||||
@ -1009,6 +1019,7 @@ class BulkDownloadView(GenericAPIView):
|
|||||||
content = serializer.validated_data.get("content")
|
content = serializer.validated_data.get("content")
|
||||||
follow_filename_format = serializer.validated_data.get("follow_formatting")
|
follow_filename_format = serializer.validated_data.get("follow_formatting")
|
||||||
|
|
||||||
|
try:
|
||||||
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
|
os.makedirs(settings.SCRATCH_DIR, exist_ok=True)
|
||||||
temp = tempfile.NamedTemporaryFile(
|
temp = tempfile.NamedTemporaryFile(
|
||||||
dir=settings.SCRATCH_DIR,
|
dir=settings.SCRATCH_DIR,
|
||||||
@ -1037,6 +1048,8 @@ class BulkDownloadView(GenericAPIView):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
except (FileNotFoundError, PermissionError) as e:
|
||||||
|
return HttpResponse(f"File IO error: {str(e)}", status=500)
|
||||||
|
|
||||||
|
|
||||||
class RemoteVersionView(GenericAPIView):
|
class RemoteVersionView(GenericAPIView):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user