v4.5 Demo initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import logging
|
||||
from functools import wraps
|
||||
from django.http import JsonResponse
|
||||
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
||||
from django.db import DatabaseError, IntegrityError
|
||||
|
||||
|
||||
def handle_exceptions(func):
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
except ValidationError as e:
|
||||
logging.error(f"Validierungsfehler in {func.__name__}: {str(e)}")
|
||||
return JsonResponse({
|
||||
'status': 'error',
|
||||
'message': 'Validierungsfehler',
|
||||
'error': str(e),
|
||||
'data': []
|
||||
}, status=400)
|
||||
|
||||
except ObjectDoesNotExist as e:
|
||||
logging.error(f"Objekt nicht gefunden in {func.__name__}: {str(e)}")
|
||||
return JsonResponse({
|
||||
'status': 'error',
|
||||
'message': 'Angeforderte Ressource nicht gefunden',
|
||||
'error': str(e),
|
||||
'data': []
|
||||
}, status=404)
|
||||
|
||||
except IntegrityError as e:
|
||||
logging.error(f"Datenbankintegritätsfehler in {func.__name__}: {str(e)}")
|
||||
return JsonResponse({
|
||||
'status': 'error',
|
||||
'message': 'Datenbankintegritätsfehler',
|
||||
'error': str(e),
|
||||
'data': []
|
||||
}, status=400)
|
||||
|
||||
except DatabaseError as e:
|
||||
logging.error(f"Datenbankfehler in {func.__name__}: {str(e)}")
|
||||
return JsonResponse({
|
||||
'status': 'error',
|
||||
'message': 'Datenbankfehler',
|
||||
'error': str(e),
|
||||
'data': []
|
||||
}, status=500)
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Unerwarteter Fehler in {func.__name__}: {str(e)}")
|
||||
return JsonResponse({
|
||||
'status': 'error',
|
||||
'message': 'Ein unerwarteter Fehler ist aufgetreten',
|
||||
'error': str(e),
|
||||
'data': []
|
||||
}, status=500)
|
||||
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user