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

Removed the custom error class again, since decoders that can fail decoding will always raise a UnicodeDecodeError according to
https://docs.python.org/3/library/codecs.html.
Modified try-catch to add a more specific note to the UnicodeError for better user information.
  • Loading branch information
JulianHn committed Jun 3, 2022
1 parent 6678a29 commit 53fdc73
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/omero/util/populate_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ class MeasurementError(Exception):
"""
pass


class DownloadingOriginalFileProvider(object):

"""
Expand All @@ -169,15 +168,21 @@ 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 as e:
e.add_note("The original file data could not be decoded assuming unicode encoding. Please specify the correct encoding used for the file!")
raise
finally:
temporary_file.close()
temporary_file.seek(0)
temporary_file.truncate(size_new)

Expand Down

0 comments on commit 53fdc73

Please sign in to comment.