From 687dc31b7a0c132f8d2ee80424381ad52c570be5 Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Wed, 21 Apr 2021 06:21:26 -0400 Subject: [PATCH] improved error message on invalid extra scripts --- cellxgene_gateway/extra_scripts.py | 8 +++++++- tests/test_extra_scripts.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cellxgene_gateway/extra_scripts.py b/cellxgene_gateway/extra_scripts.py index abf2218..ac05676 100644 --- a/cellxgene_gateway/extra_scripts.py +++ b/cellxgene_gateway/extra_scripts.py @@ -8,6 +8,7 @@ # the specific language governing permissions and limitations under the License. from json import loads +from json.decoder import JSONDecodeError from cellxgene_gateway import env @@ -17,4 +18,9 @@ def get_extra_scripts(): # ['https://www.googletagmanager.com/gtag/js?id=UA-123456-2', # f"{env.external_protocol}://{env.external_host}/static/js/google_ua.js"] # where google_ua.js is a script you add to the static/js folder prior to deployment. - return [] if env.extra_scripts is None else loads(env.extra_scripts) + try: + return [] if env.extra_scripts is None else loads(env.extra_scripts) + except JSONDecodeError as exc: + raise Exception( + f'Error parsing GATEWAY_EXTRA_SCRIPTS, expected JSON array e.g. ["https://example.com/path/to/script.js"]' + ) from exc diff --git a/tests/test_extra_scripts.py b/tests/test_extra_scripts.py index c429c55..97020ed 100644 --- a/tests/test_extra_scripts.py +++ b/tests/test_extra_scripts.py @@ -21,6 +21,15 @@ class TestExtraScripts(unittest.TestCase): def test_GIVEN_empty_string_THEN_returns_empty_array(self): self.assertEqual(get_extra_scripts(), []) + @patch("cellxgene_gateway.env.extra_scripts", new="'asdf'") + def test_GIVEN_bare_string_THEN_throws_Exception(self): + with self.assertRaises(Exception) as context: + self.assertEqual(get_extra_scripts(), []) + self.assertEqual( + 'Error parsing GATEWAY_EXTRA_SCRIPTS, expected JSON array e.g. ["https://example.com/path/to/script.js"]', + str(context.exception), + ) + if __name__ == "__main__": unittest.main()