首页 > 解决方案 > Django-rest-framework 中的全局异常处理

问题描述

有没有办法在不使用 django rest 框架中的 try-except 块的情况下全局处理所有异常。

我想将 django 抛出的 html 错误页面转换为自定义的 json 对象响应。

我在我的应用程序中创建了一个 exception.py 文件

def custom_exception_handler(exc, context=None):
    response = exception_handler(exc)


    if isinstance(exc, HttpResponseServerError):  
        custom_response_data = { 
            'detail': 'Internal Server Error' # custom exception message
        }
        response.data = custom_response_data

    return response

我已经在 settings.py 中配置了这个。

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER':'my_project.my_app.exceptions.custom_exception_handler'}

标签: djangoexceptiondjango-rest-frameworkdjango-rest-viewsetsdjango-errors

解决方案


由于我遇到了导致我提出这个问题的类似情况,因此我将按照与 Django Rest Framework 相关的原始问题进行回答,而不仅仅是 Django。

我了解您希望全局处理视图中引发的异常,而无需在每个视图模块上定义 try/except 块。

DRF 允许您定义自己的自定义异常处理机制(文档)。这是一个例子:

在 my_custom_except_handler.py:

import logging
from rest_framework.views import exception_handler
from django.http import JsonResponse
from requests import ConnectionError

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first
    response = exception_handler(exc, context)

    # checks if the raised exception is of the type you want to handle
    if isinstance(exc, ConnectionError):
        # defines custom response data
        err_data = {'MSG_HEADER': 'some custom error messaging'}

        # logs detail data from the exception being handled
        logging.error(f"Original error detail and callstack: {exc}")
        # returns a JsonResponse
        return JsonResponse(err_data, safe=False, status=503)

    # returns response as handled normally by the framework
    return response

如文档中所述,定义的响应对象是指:

异常处理函数应该返回一个 Response 对象,或者如果无法处理异常,则返回 None。如果处理程序返回 None 那么异常将被重新引发并且 Django 将返回一个标准的 HTTP 500“服务器错误”响应。

换句话说,只有在处理这些异常文档时,'response' 才不会是 None :

  • APIException 的子类。
  • Django 的 Http404 异常。
  • Django 的 PermissionDenied 异常。

如果您的自定义处理程序返回 None,那么框架将“正常”处理异常,返回典型的 500 服务器错误。

最后记得在settings.py中设置需要的key:

REST_FRAMEWORK = {'EXCEPTION_HANDLER': 
    'my_project.my_app.my_custom_except_handler.custom_exception_handler'}

希望能帮助到你!


推荐阅读