Skip to content

Commit

Permalink
[PATCH] Minor patch in middlewares (#1397) (#1398)
Browse files Browse the repository at this point in the history
* [FEAT] Configure CustomException Error handling

* [PATCH] Log non-JSON data (#1388)

* [PATCH] Minor patch in middlewares

---------

Co-authored-by: Adnan Kattekaden <[email protected]>
Co-authored-by: Aashish Vinu <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2023
1 parent 3efc1ce commit e781150
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions mulearnbackend/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from contextlib import suppress
import hmac
import json
import json
import logging
import traceback
from contextlib import suppress

import decouple
from django.conf import settings
Expand Down Expand Up @@ -95,7 +96,7 @@ def __call__(self, request):

def log_exception(self, request, exception):
"""
Log the exception and prints the information in CLI if DEBUG is True.
Log the exception and prints the information in CLI.
Args:
request: The request object.
Expand All @@ -109,24 +110,27 @@ def log_exception(self, request, exception):
)
logger.error(error_message)

if settings.DEBUG:
print(error_message)
print(error_message)

data = request.body.decode('utf-8')) if hasattr(request, 'body') else 'No Data'
body = request.body.decode("utf-8") if hasattr(request, "body") else "No body"
auth = request.auth if hasattr(request, "auth") else "No Auth data"

with suppress(json.JSONDecodeError):
body = json.loads(body)
body = json.dumps(body, indent=4)

with suppress(json.JSONDecodeError):
data = json.loads(data)
auth = json.dumps(auth, indent=4)

request_info = (
f"Request Info: METHOD: {request.method}; \n"
f"\tPATH: {request.path}; \n"
f"\tDATA: {data}\n"
f"PATH: {request.path}; \n"
f"AUTH: \n{auth} \n"
f"BODY: \n{body}\n"
)
logger.error(request_info)

# Print to terminal if DEBUG is True
if settings.DEBUG:
print(request_info)
print(request_info)

def process_exception(self, request, exception):
"""
Expand Down
2 changes: 1 addition & 1 deletion mulearnbackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.common.CommonMiddleware",
"corsheaders.middleware.CorsMiddleware",
# "mulearnbackend.middlewares.UniversalErrorHandlerMiddleware",
"mulearnbackend.middlewares.UniversalErrorHandlerMiddleware",
]

ROOT_URLCONF = "mulearnbackend.urls"
Expand Down

0 comments on commit e781150

Please sign in to comment.