|
18 | 18 | """
|
19 | 19 |
|
20 | 20 | import logging
|
| 21 | +from http import HTTPStatus |
21 | 22 | from threading import local
|
22 | 23 |
|
23 | 24 | from django.contrib import messages
|
24 | 25 | from django.db.models import prefetch_related_objects
|
| 26 | +from django.http import JsonResponse |
| 27 | +from rest_framework import views, exceptions |
25 | 28 |
|
26 |
| -from promgen import models |
| 29 | +from promgen import models, settings |
27 | 30 | from promgen.signals import trigger_write_config, trigger_write_rules, trigger_write_urls
|
28 | 31 |
|
29 | 32 | logger = logging.getLogger(__name__)
|
@@ -67,3 +70,18 @@ def __call__(self, request):
|
67 | 70 |
|
68 | 71 | def get_current_user():
|
69 | 72 | return getattr(_user, "value", None)
|
| 73 | + |
| 74 | + |
| 75 | +def custom_exception_handler(exc, context): |
| 76 | + # Call REST framework's default exception handler first, |
| 77 | + # to get the standard error response. |
| 78 | + response = views.exception_handler(exc, context) |
| 79 | + |
| 80 | + if response is None: |
| 81 | + # If an exception is raised that we don't handle, we will return a 500 error |
| 82 | + # with the exception message. This is useful for debugging in development |
| 83 | + if settings.DEBUG: |
| 84 | + return JsonResponse({"detail": str(exc)}, status=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 85 | + return exceptions.server_error(context["request"]) |
| 86 | + |
| 87 | + return response |
0 commit comments