我的网站有时会触发RecursionError
错误。不幸的是,错误日志没有帮助。
奇怪的是,response = self.handle_exception(exc)
中的except
被调用了,但是之后,代码从response = handler(request, *args, **kwargs)
的上一行继续执行
def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate?
try:
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
response = handler(request, *args, **kwargs)
except Exception as exc:
response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
发生异常时,代码是否应该跳出try
块?
回溯
Traceback:
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/viewsets.py" in view
116. return self.dispatch(request, *args, **kwargs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
495. response = self.handle_exception(exc)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception
455. self.raise_uncaught_exception(exc)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/views.py" in dispatch
492. response = handler(request, *args, **kwargs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/mixins.py" in list
45. return self.get_paginated_response(serializer.data)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in data
765. ret = super(ListSerializer, self).data
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in data
262. self._data = self.to_representation(self.instance)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
683. self.child.to_representation(item) for item in iterable
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in <listcomp>
683. self.child.to_representation(item) for item in iterable
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
527. ret[field.field_name] = field.to_representation(attribute)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/serializers.py" in to_representation
514. attribute = field.get_attribute(instance)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/fields.py" in get_attribute
454. return get_attribute(instance, self.source_attrs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/rest_framework/fields.py" in get_attribute
99. if isinstance(instance, collections.Mapping):
File "/usr/lib/python3.6/abc.py" in __instancecheck__
193. return cls.__subclasscheck__(subclass)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
File "/usr/lib/python3.6/typing.py" in __extrahook__
884. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
File "/usr/lib/python3.6/typing.py" in __extrahook__
884. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
File "/usr/lib/python3.6/typing.py" in __extrahook__
884. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
它持续进行了几百行,然后我有了:
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
File "/usr/lib/python3.6/typing.py" in __extrahook__
884. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
222. for rcls in cls._abc_registry:
File "/usr/lib/python3.6/_weakrefset.py" in __iter__
59. with _IterationGuard(self):
During handling of the above exception (maximum recursion depth exceeded), another exception occurred:
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
91. response = response or self.get_response(request)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
36. response = response_for_exception(request, exc)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in response_for_exception
90. response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in handle_uncaught_exception
129. return callback(request, **param_dict)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/views/defaults.py" in server_error
69. template = loader.get_template(template_name)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/template/loader.py" in get_template
12. engines = _engine_list(using)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/template/loader.py" in _engine_list
66. return engines.all() if using is None else [engines[using]]
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/template/utils.py" in all
90. return [self[alias] for alias in self]
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/template/utils.py" in __iter__
87. return iter(self.templates)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/path/to/webapp/venv/lib/python3.6/site-packages/django/template/utils.py" in templates
54. counts = Counter(backend_names)
File "/usr/lib/python3.6/collections/__init__.py" in __init__
535. self.update(*args, **kwds)
File "/usr/lib/python3.6/collections/__init__.py" in update
614. if isinstance(iterable, Mapping):
File "/usr/lib/python3.6/abc.py" in __instancecheck__
193. return cls.__subclasscheck__(subclass)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
209. ok = cls.__subclasshook__(subclass)
File "/usr/lib/python3.6/typing.py" in __extrahook__
884. if issubclass(subclass, scls):
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
228. if issubclass(subclass, scls):
File "/usr/lib/python3.6/typing.py" in __subclasscheck__
1154. return super().__subclasscheck__(cls)
File "/usr/lib/python3.6/abc.py" in __subclasscheck__
此模式在数千行中重复几次,直到收到maximum recursion depth exceeded while calling a Python object
错误