From bf3e8227a902ca0828c9a66130064a23d5f75163 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Mon, 30 Dec 2019 22:29:01 +0200 Subject: [PATCH] Fixed #31166 -- Used "raise from" when raising ImproperlyConfigured exceptions in django.urls.resolvers. This change sets the __cause__ attribute to raised exceptions and makes small cleanups in error messages. --- django/urls/resolvers.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 6e3f2443c9..b6b9d6fb2b 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -190,7 +190,7 @@ class RegexPattern(CheckURLMixin): except re.error as e: raise ImproperlyConfigured( '"%s" is not a valid regular expression: %s' % (regex, e) - ) + ) from e def __str__(self): return str(self._regex) @@ -234,8 +234,9 @@ def _route_to_regex(route, is_endpoint=False): converter = get_converter(raw_converter) except KeyError as e: raise ImproperlyConfigured( - "URL route '%s' uses invalid converter %s." % (original_route, e) - ) + 'URL route %r uses invalid converter %r.' + % (original_route, raw_converter) + ) from e converters[parameter] = converter parts.append('(?P<' + parameter + '>' + converter.regex + ')') if is_endpoint: @@ -588,13 +589,13 @@ class URLResolver: patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) try: iter(patterns) - except TypeError: + except TypeError as e: msg = ( "The included URLconf '{name}' does not appear to have any " "patterns in it. If you see valid patterns in the file then " "the issue is probably caused by a circular import." ) - raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) + raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e return patterns def resolve_error_handler(self, view_type):