Django: помощь в реализации отчетов об ошибках

Я пытаюсь реализовать систему отчетов об ошибках, которая напрямую отправляет ошибки в slack. Шаги выглядят следующим образом:

  1. My API fails and return a 400 error.
  2. The Middleware will check if the response code equals 400
  3. Call the function to send the error message to slack.

API:

class SomeWorkingAPI(APIView):
    permission_classes = (AllowAny,)

    def get(self, request):
        client = request.query_params.get('client', None)
            try:
                #do something
            except:
                error_msg = "error occurred in the API"
                return Response({'message': error_msg}, status=status.HTTP_400_BAD_REQUEST)
        return Response(result, status=status.HTTP_200_OK)

Middleware:

def error_reporting(request, error_msg=None):
    # this is the function that sends the message to my slack channel
    return 'error reported successfully'

class SomeMiddleWare(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_template_response(self, request, response):
        if response.status_code >= 400:
            error_reporting(request, error_msg=response['message'])

Помощь, которая мне нужна:

  1. I want to send the error message returned from the API (return Response({'message': error_msg}, status=status.HTTP_400_BAD_REQUEST)) to the middleware. How can i achieve that? What i'm doing in middleware (response['message']), is throwing KeyError.
  2. Since the middleware has both request and response, it would be great if I can get all the query params and payload from the request. When I'm using request.query_params in the error_reporting function, I'm getting AttributeError: 'WSGIRequest' object has no attribute 'query_params'.
  3. It would be great if you can provide some other way to achieve my goal. Thank you
Вернуться на верх