mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 11:52:22 -06:00
Move utilities.api.rest_api_server_error() to utilities.error_handlers.handle_rest_api_exception()
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
import platform
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.db.models import ProtectedError, RestrictedError
|
||||
from django.http import JsonResponse
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import status
|
||||
|
||||
__all__ = (
|
||||
'handle_protectederror',
|
||||
'handle_rest_api_exception',
|
||||
)
|
||||
|
||||
|
||||
def handle_protectederror(obj_list, request, e):
|
||||
@@ -32,3 +43,17 @@ def handle_protectederror(obj_list, request, e):
|
||||
err_message += ', '.join(dependent_objects)
|
||||
|
||||
messages.error(request, mark_safe(err_message))
|
||||
|
||||
|
||||
def handle_rest_api_exception(request, *args, **kwargs):
|
||||
"""
|
||||
Handle exceptions and return a useful error message for REST API requests.
|
||||
"""
|
||||
type_, error, traceback = sys.exc_info()
|
||||
data = {
|
||||
'error': str(error),
|
||||
'exception': type_.__name__,
|
||||
'netbox_version': settings.VERSION,
|
||||
'python_version': platform.python_version(),
|
||||
}
|
||||
return JsonResponse(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
Reference in New Issue
Block a user