rancher check unique - pos free products check unique - 500 error handling

This commit is contained in:
2025-08-31 11:48:14 +03:30
parent 9d9d4d3b80
commit dd5be869c5
7 changed files with 61 additions and 64 deletions

View File

@@ -1,5 +1,7 @@
from django.http import JsonResponse
from rest_framework.views import exception_handler
from django.conf import settings
import traceback
def custom_exception_handler(exc, context):
@@ -11,6 +13,13 @@ def custom_exception_handler(exc, context):
response.data['message'] = response.data.get('detail', str(exc))
del response.data['detail']
else:
response = JsonResponse({'message': str(exc), 'status_code': 500})
if settings.DEBUG:
raise
response = JsonResponse({
"message": str(exc),
"status_code": 500,
"error_type": exc.__class__.__name__,
"traceback": traceback.format_exc().splitlines(),
}, status=500)
response.status_code = 500
return response

View File

@@ -1,60 +1,29 @@
from rest_framework.exceptions import APIException
from django.http import JsonResponse
from rest_framework import status
from django.db import connection
from django.conf import settings
import traceback
import os
def terminal_width():
"""
Function to compute the terminal width.
WARNING: This is not my code, but I've been using it forever and
I don't remember where it came from.
"""
width = 0
try:
import struct, fcntl, termios
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
width = struct.unpack('HHHH', x)[1]
except:
pass
if width <= 0:
try:
width = int(os.environ['COLUMNS'])
except:
pass
if width <= 0:
width = 80
return width
class SqlPrintingMiddleware(object):
"""
Middleware which prints out a list of all SQL queries done
for each view that is processed. This is only useful for debugging.
"""
class Json500Middleware:
""" return all 500 status errors in json response """
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.process_response(request=request, response=self.get_response(request))
def __call__(self, request, *args, **kwargs):
try:
return self.get_response(request)
except Exception as exc:
def process_response(self, request, response): # noqa
indentation = 2
if len(connection.queries) > 0 and settings.DEBUG:
width = terminal_width()
total_time = 0.0
for query in connection.queries:
nice_sql = query['sql'].replace('"', '').replace(',', ', ')
sql = "\033[1;31m[%s]\033[0m %s" % (query['time'], nice_sql)
total_time = total_time + float(query['time'])
while len(sql) > width - indentation:
print("%s%s" % (" " * indentation, sql[:width - indentation]))
sql = sql[width - indentation:]
print("%s%s\n" % (" " * indentation, sql))
replace_tuple = (" " * indentation, str(total_time))
print("%s\033[1;32m[TOTAL TIME: %s seconds]\033[0m" % replace_tuple)
print(response)
return response
if settings.DEBUG:
raise
return JsonResponse({
"message": str(exc),
"status_code": 500,
"error_type": exc.__class__.__name__,
"traceback": traceback.format_exc().splitlines(),
}, status=500)