Django: помощь в реализации отчетов об ошибках
Я пытаюсь реализовать систему отчетов об ошибках, которая напрямую отправляет ошибки в slack. Шаги выглядят следующим образом:
- My API fails and return a 400 error.
- The Middleware will check if the response code equals 400
- 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'])
Помощь, которая мне нужна:
- 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. - 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 theerror_reporting
function, I'm gettingAttributeError: 'WSGIRequest' object has no attribute 'query_params'
. - It would be great if you can provide some other way to achieve my goal. Thank you