From ee0e095f07e8ea49397f8a5d83813fc9034bc595 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 8 Aug 2017 09:44:56 +0200 Subject: [PATCH] distinguish invalid files from parse errors in restore 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 | 12 +++++++++++- 1 files changed, 11 insertions(+), 1 deletions(-) diff --git a/deltatar/deltatar.py b/deltatar/deltatar.py index b2d9c31..2d48c39 100644 --- a/deltatar/deltatar.py +++ b/deltatar/deltatar.py @@ -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)) -- 1.7.1