From 01e8e09d40f736f78ab107aae983b6e895ffc4d7 Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Tue, 20 Sep 2022 15:45:36 +0100 Subject: [PATCH] schema: Add error checking on remote schema files When fetching and downloading we can get some obscure messages that are hard to distinguish between input data errors and configuration errors. (As seen with recent threesixtygiving test coves). This captures errors and rewords them to be more helpful. --- flattentool/schema.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/flattentool/schema.py b/flattentool/schema.py index b572501..166faee 100644 --- a/flattentool/schema.py +++ b/flattentool/schema.py @@ -147,12 +147,29 @@ def __init__( ) if schema_filename: if schema_filename.startswith("http"): + import json + import requests r = requests.get(schema_filename) - self.root_schema_dict = jsonref.loads( - r.text, object_pairs_hook=OrderedDict - ) + + try: + r.raise_for_status() + except requests.HTTPError: + raise ValueError( + _( + "The URL provided for the schema in schema_filename was not accessible" + ) + ) + + try: + self.root_schema_dict = jsonref.loads( + r.text, object_pairs_hook=OrderedDict + ) + except json.JSONDecodeError: + raise ValueError( + _("The schema provided in schema_filename was not valid JSON") + ) else: if disable_local_refs: with codecs.open(schema_filename, encoding="utf-8") as schema_file: