distinguish invalid files from parse errors in restore
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 8 Aug 2017 07:44:56 +0000 (09:44 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
Especially with index files, the parse error is misleading.
Indicate the prevalent cause of the problem, i. e. that the
file is compressed but compression was not requested during
restore.

deltatar/deltatar.py

index b2d9c31..2d48c39 100644 (file)
@@ -1447,12 +1447,22 @@ class DeltaTar(object):
 
     def _parse_json_line(self, f, l_no):
         '''
-        read from a file and parses a json line and prints it on screen on error
+        Read line from file like object and process it as JSON.
         '''
         l = f.readline()
         l_no += 1
         try:
             j = json.loads(l.decode('UTF-8'))
+        except UnicodeDecodeError as e:
+            if tuple (l [0:2]) == tarfile.GZ_MAGIC:
+                raise Exception \
+                    ("error parsing line #%d as json: looks like a compressed file (%d B: [%s..])"
+                     % (l_no, len (l), binascii.hexlify (l [:16]).decode ())) \
+                    from e
+            raise Exception \
+                ("error parsing line #%d as json: not a text file (%d B: [%s..])"
+                 % (l_no, len (l), binascii.hexlify (l [:16]).decode ())) \
+                from e
         except ValueError as e:
             raise Exception("error parsing this json line "
                 "(line number %d): %s" % (l_no, l))