简体   繁体   English

如何将自定义错误代码添加到Django Rest Framework

[英]How to add custom error codes to Django Rest Framework

I am putting together an API with Django Rest Framework. 我正在将API与Django Rest Framework组合在一起。 I want to customise my error handling. 我想自定义我的错误处理。 I read quite a bit ( link1 , link2 , link3 ) about custom error handling but can't find something that suits my needs. 我阅读了很多有关自定义错误处理的内容( link1link2link3 ),但是找不到适合我需求的内容。

Basically, I'd like to change the structure of my error messages to get something like this : 基本上,我想更改错误消息的结构以获取如下信息:

{
  "error": True,
  "errors": [
    {
      "message": "Field %s does not exist",
      "code": 1050
    }
  ]
}

Instead of : 代替 :

{"detail":"Field does not exist"}

I already have a custom ExceptionMiddleware to catch the 500 errors and return a JSON, but I have no power on all the other errors. 我已经有一个自定义ExceptionMiddleware来捕获500个错误并返回JSON,但是我无法处理所有其他错误。

Code of the ExceptionMiddleware: ExceptionMiddleware的代码:

class ExceptionMiddleware(object):

    def process_exception(self, request, exception):

        if request.user.is_staff:
            detail = exception.message
        else:
            detail = 'Something went wrong, please contact a staff member.'

        return HttpResponse('{"detail":"%s"}'%detail, content_type="application/json", status=500)

From Django doc : 从Django doc中:

Note that the exception handler will only be called for responses generated by raised exceptions. 请注意,将仅针对由引发的异常生成的响应调用异常处理程序。 It will not be used for any responses returned directly by the view, such as the HTTP_400_BAD_REQUEST responses that are returned by the generic views when serializer validation fails. 它不会用于视图直接返回的任何响应,例如,当序列化程序验证失败时,通用视图返回的HTTP_400_BAD_REQUEST响应。

This is exactly what I am trying to achieve, customise those 400 errors. 这正是我想要实现的,自定义这400个错误。

Thanks a lot, 非常感谢,

I know this is a bit late, ( better late than never ). 我知道这有点晚了( 迟到总比没有好 )。

If you have a structured error message, then try this by inheriting the Exception class 如果您有结构化的错误消息,请通过继承Exception类来尝试此操作

from rest_framework.serializers import ValidationError
from rest_framework import status


class CustomAPIException(ValidationError):
    status_code = status.HTTP_400_BAD_REQUEST
    default_code = 'error'

    def __init__(self, detail, status_code=None):
        self.detail = detail
        if status_code is not None:
            self.status_code = status_code

and the usage will be like this: 用法将是这样的:

if some_condition:
    error_msg = {
        "error": True,
        "errors": [
            {
                "message": "Field %s does not exist"%('my_test_field'),
                "code": 1050
            }
        ]
    }
    raise CustomAPIException(error_msg)

Reference : How to override exception messages in django rest framework 参考: 如何在Django Rest框架中覆盖异常消息

The exception handler is indeed what you're looking for. 异常处理程序确实是您要找的东西。 The current mixing do raise an exception in case of failed validation ( https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py ). 当前的混合确实会在验证失败的情况下引发异常( https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py )。

Note that the exception handler will only be called for responses generated by raised exceptions. 请注意,将仅针对由引发的异常生成的响应调用异常处理程序。 It will not be used for any responses returned directly by the view, such as the HTTP_400_BAD_REQUEST responses that are returned by the generic views when serializer validation fails. 它不会用于视图直接返回的任何响应,例如,当序列化程序验证失败时,通用视图返回的HTTP_400_BAD_REQUEST响应。

I think this part doesn't hold any longer and should be rephrased by removing the "generic" word. 我认为这部分不再适用,应通过删除“通用”一词来重新措辞。

This is my custom exception handler: 这是我的自定义异常处理程序:

def api_exception_handler(exception, context):

    if isinstance(exception, exceptions.APIException):

        headers = {}

        if getattr(exception, 'auth_header', None):
            headers['WWW-Authenticate'] = exception.auth_header

        if getattr(exception, 'wait', None):
            headers['Retry-After'] = '%d' % exception.wait

        data = exception.get_full_details()
        set_rollback()

        return Response(data, status=exception.status_code, headers=headers)

    return exception_handler(exception, context)

It represents APIException errors in a format like this: 它以APIException格式表示APIException错误:

{
    "field_name": [
        {
            "message": "Error message",
            "code": "error_code"
        },
        {
            "message": "Another error",
            "code": "another_error"
        }
    ]
}

Django Rest Framework reference documentation: Django Rest Framework参考文档:
http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling http://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling

Alright so based on Linovia's answer, here is my custom handler : 好吧,基于Linovia的回答,这是我的自定义处理程序:

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)
    if response is not None:
        errors = []
        for msg in response.data.values():
            errors.append({'message': msg[0], 'error': get_error_message()})

        response.data = {"errors": errors}

    return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM