Skip to content

Commit d2fe59a

Browse files
committed
Fixes #7109: Ensure human readability of exceptions raised during REST API requests
1 parent f63dcb1 commit d2fe59a

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [#7101](https://github.com/netbox-community/netbox/issues/7101) - Enforce `MAX_PAGE_SIZE` for table and REST API pagination
2424
* [#7106](https://github.com/netbox-community/netbox/issues/7106) - Fix incorrect "Map It" button URL on a site's Physical Address field
2525
* [#7107](https://github.com/netbox-community/netbox/issues/7107) - Fix missing search button and search results in IP Address assignment "Assign IP" tab
26+
* [#7109](https://github.com/netbox-community/netbox/issues/7109) - Ensure human readability of exceptions raised during REST API requests
2627

2728
---
2829

netbox/netbox/middleware.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def __call__(self, request):
113113

114114
def process_exception(self, request, exception):
115115

116+
# Handle exceptions that occur from REST API requests
117+
if is_api_request(request):
118+
return rest_api_server_error(request)
119+
116120
# Don't catch exceptions when in debug mode
117121
if settings.DEBUG:
118122
return
@@ -121,10 +125,6 @@ def process_exception(self, request, exception):
121125
if isinstance(exception, Http404):
122126
return
123127

124-
# Handle exceptions that occur from REST API requests
125-
if is_api_request(request):
126-
return rest_api_server_error(request)
127-
128128
# Determine the type of exception. If it's a common issue, return a custom error page with instructions.
129129
custom_template = None
130130
if isinstance(exception, ProgrammingError):

netbox/utilities/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def is_api_request(request):
4848
Return True of the request is being made via the REST API.
4949
"""
5050
api_path = reverse('api-root')
51-
return request.path_info.startswith(api_path)
51+
52+
return request.path_info.startswith(api_path) and request.content_type == 'application/json'
5253

5354

5455
def get_view_name(view, suffix=None):

0 commit comments

Comments
 (0)