Skip to content

Commit

Permalink
Added custom exception and try-catch around the Decoding Part in get_…
Browse files Browse the repository at this point in the history
…original_file_data() to provide the user with more helpful error messages as well as enabling easy catching of the exception in calling scripts
  • Loading branch information
JulianHn committed Jun 2, 2022
1 parent 6678a29 commit cd088f5
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/omero/util/populate_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ class MeasurementError(Exception):
"""
pass

class DecodingError(Exception):

"""
Raised if an error occurs during decoding original file data
"""
pass

class DownloadingOriginalFileProvider(object):

Expand Down Expand Up @@ -169,15 +175,22 @@ def get_original_file_data(self, original_file, encoding="utf-8"):
dir=str(self.dir))

size = original_file.size.val
if encoding != "utf-8": size_new = 0 # Needed to keep track of reencoded file size if encoding is not utf-8 already
if encoding != "utf-8": size_new = 0 # Needed to keep track of re-encoded file size if encoding is not utf-8 already
else: size_new = size # Else can be the same as the old data size

for i in range((old_div(size, self.BUFFER_SIZE)) + 1):
index = i * self.BUFFER_SIZE
data = self.raw_file_store.read(index, self.BUFFER_SIZE)
data_write = data.decode(encoding)
if encoding != "utf-8": size_new += len(data_write.encode("utf-8")) # Track total size
temporary_file.write(data_write)
try:
for i in range((old_div(size, self.BUFFER_SIZE)) + 1):
index = i * self.BUFFER_SIZE
data = self.raw_file_store.read(index, self.BUFFER_SIZE)
data_write = data.decode(encoding)
if encoding != "utf-8": size_new += len(data_write.encode("utf-8")) # Track total size
temporary_file.write(data_write)
except UnicodeDecodeError:
raise DecodingError("The original file data could not be decoded assuming unicode encoding. Please specify the correct encoding used for the file!")
except:
raise DecodingError("An unknown exception has occured during decoding of the orginal file data. Please specify the encoding used for the file!")
finally:
temporary_file.close()
temporary_file.seek(0)
temporary_file.truncate(size_new)

Expand Down

0 comments on commit cd088f5

Please sign in to comment.