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.
This commit is contained in:
Ram Rachum 2019-12-30 22:29:01 +02:00 committed by Mariusz Felisiak
parent 73563183c2
commit bf3e8227a9
1 changed files with 6 additions and 5 deletions

View File

@ -190,7 +190,7 @@ class RegexPattern(CheckURLMixin):
except re.error as e: except re.error as e:
raise ImproperlyConfigured( raise ImproperlyConfigured(
'"%s" is not a valid regular expression: %s' % (regex, e) '"%s" is not a valid regular expression: %s' % (regex, e)
) ) from e
def __str__(self): def __str__(self):
return str(self._regex) return str(self._regex)
@ -234,8 +234,9 @@ def _route_to_regex(route, is_endpoint=False):
converter = get_converter(raw_converter) converter = get_converter(raw_converter)
except KeyError as e: except KeyError as e:
raise ImproperlyConfigured( 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 converters[parameter] = converter
parts.append('(?P<' + parameter + '>' + converter.regex + ')') parts.append('(?P<' + parameter + '>' + converter.regex + ')')
if is_endpoint: if is_endpoint:
@ -588,13 +589,13 @@ class URLResolver:
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
try: try:
iter(patterns) iter(patterns)
except TypeError: except TypeError as e:
msg = ( msg = (
"The included URLconf '{name}' does not appear to have any " "The included URLconf '{name}' does not appear to have any "
"patterns in it. If you see valid patterns in the file then " "patterns in it. If you see valid patterns in the file then "
"the issue is probably caused by a circular import." "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 return patterns
def resolve_error_handler(self, view_type): def resolve_error_handler(self, view_type):