Commit 794f8152 authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Wrap jsondiff calls with a try-except to prevent fatal errors. (Closes: #903447, #903449)

parent 977e4870
Loading
Loading
Loading
Loading
Loading
+21 −12
Original line number Diff line number Diff line
@@ -66,18 +66,7 @@ class JSONFile(File):
        )

        if difference:
            if jsondiff is not None:
                a = getattr(self, 'parsed', {})
                b = getattr(other, 'parsed', {})

                diff = {repr(x): y for x, y in jsondiff.diff(a, b).items()}

                difference.add_comment("Similarity: {}%".format(
                    jsondiff.similarity(a, b),
                ))
                difference.add_comment("Differences: {}".format(
                    json.dumps(diff, indent=2, sort_keys=True),
                ))
            self.compare_with_jsondiff(difference, other)

            return [difference]

@@ -91,6 +80,26 @@ class JSONFile(File):

        return [difference]

    def compare_with_jsondiff(self, difference, other):
        if jsondiff is None:
            return

        a = getattr(self, 'parsed', {})
        b = getattr(other, 'parsed', {})

        try:
            diff = {repr(x): y for x, y in jsondiff.diff(a, b).items()}
        except Exception:
            return

        difference.add_comment("Similarity: {}%".format(
            jsondiff.similarity(a, b),
        ))

        difference.add_comment("Differences: {}".format(
            json.dumps(diff, indent=2, sort_keys=True),
        ))

    @staticmethod
    def dumps(file, sort_keys=True):
        if not hasattr(file, 'parsed'):