Detect binary file uploads
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 5 Jan 2021 19:31:39 +0000 (13:31 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 5 Jan 2021 19:31:39 +0000 (13:31 -0600)
src/pgwui_core/core.py
src/pgwui_core/exceptions.py

index ab17aeec76cf18fb863b76a5d7bd2ef453ca9ad1..4fd045cda54c253be9efa45a94574ef700c6ec44 100644 (file)
@@ -893,6 +893,11 @@ class UploadData(DBData):
                 line = next(self._fileo)
             except StopIteration:
                 raise core_ex.NoDataError('Uploaded file contains no data')
+            except UnicodeError as ex:
+                raise core_ex.CantDecodeError(
+                    'Not a text file',
+                    ("The file's content is not recognized as Unicode text, "
+                     f'the error is: {ex}'))
             else:
                 self.lineno += 1
                 # Intuit the eol sequence
index ea618ed6f12cd6aed91e967f7df3dba34b4114df..22cb2a6d47d80be0ff73ba927de272e0bffce413 100644 (file)
@@ -149,6 +149,12 @@ class NoDataError(Error):
         super(NoDataError, self).__init__(e, descr, detail)
 
 
+class CantDecodeError(Error):
+    '''Can't decode file data into unicode'''
+    def __init__(self, e, descr='', detail=''):
+        super().__init__(e, descr, detail)
+
+
 class DuplicateUploadError(Error):
     '''The same filename updated twice into the same db'''
     def __init__(self, e, descr='', detail=''):