From e47382d4abb8ced3f4c7956b60c55d82b7c87d52 Mon Sep 17 00:00:00 2001 From: Tom Parker-Shemilt Date: Wed, 21 Dec 2022 08:19:39 +0000 Subject: [PATCH] Better error on missing schema (#196) --- dj_database_url.py | 8 +++++++- test_dj_database_url.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/dj_database_url.py b/dj_database_url.py index 39f7405..ac194a4 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -115,7 +115,13 @@ def parse( hostname = urlparse.unquote(hostname) # Lookup specified engine. - engine = SCHEMES[url.scheme] if engine is None else engine + if engine is None: + engine = SCHEMES.get(url.scheme) + if engine is None: + raise ValueError( + "No support for '%s'. We support: %s" + % (url.scheme, ", ".join(sorted(SCHEMES.keys()))) + ) port = ( str(url.port) diff --git a/test_dj_database_url.py b/test_dj_database_url.py index a10625a..0ed8b02 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -541,6 +541,10 @@ def test_persistent_connection_variables_config(self): assert url["CONN_MAX_AGE"] == 600 assert url["CONN_HEALTH_CHECKS"] is True + def test_bad_url_parsing(self): + with self.assertRaisesRegex(ValueError, "No support for 'foo'. We support: "): + dj_database_url.parse("foo://bar") + if __name__ == "__main__": unittest.main()