Uncaught Exceptions from within ModelViewSet are returned as HTML #5278

Closed
opened 2025-12-29 19:26:08 +01:00 by adam · 2 comments
Owner

Originally created by @thatmattlove on GitHub (Sep 1, 2021).

Originally assigned to: @thatmattlove on GitHub.

NetBox version

v3.0.0

Python version

3.8

Steps to Reproduce

This can be tricky - something needs to done to trigger an uncaught exception from within ModelViewSet. I suppose you could add this...

raise Exception('test')

...here, and then make an API request.

Expected Behavior

At a minimum, I think we should append useful information to the error. Ideally, however, no HTML would be returned to an API client.

Observed Behavior

The returned value is something along the lines of:

{
  "error": "<giant unreadable HTML string>"
}
Originally created by @thatmattlove on GitHub (Sep 1, 2021). Originally assigned to: @thatmattlove on GitHub. ### NetBox version v3.0.0 ### Python version 3.8 ### Steps to Reproduce This can be tricky - something needs to done to trigger an uncaught exception from within `ModelViewSet`. I suppose you could add this... ```python raise Exception('test') ``` ...[here](https://github.com/netbox-community/netbox/blob/d743dc160ab701cb6b92579b5ff164b5ec2eaa9b/netbox/netbox/api/views.py#L199), and then make an API request. ### Expected Behavior At a minimum, I think we should append useful information to the error. Ideally, however, no HTML would be returned to an API client. ### Observed Behavior The returned value is something along the lines of: ```json { "error": "<giant unreadable HTML string>" } ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:26:08 +01:00
adam closed this issue 2025-12-29 19:26:08 +01:00
Author
Owner

@jeremystretch commented on GitHub (Sep 1, 2021):

Any exceptions raised by an API view should get caught and handled by ExceptionHandlingMiddleware (unless debugging is enabled). I can confirm that this works as expected when intentionally raising an exception in ModelViewSet.dispatch().

Can we just extend the middleware to evaluate the request type and act accordingly? I.e. invoke rest_api_server_error() only if it's an API request and JSON content was requested?

@jeremystretch commented on GitHub (Sep 1, 2021): Any exceptions raised by an API view should get caught and handled by [ExceptionHandlingMiddleware](https://github.com/netbox-community/netbox/blob/f63dcb1f08d85c59eb8b995a0293400036dc83dd/netbox/netbox/middleware.py#L103) (unless debugging is enabled). I can confirm that this works as expected when intentionally raising an exception in `ModelViewSet.dispatch()`. Can we just extend the middleware to evaluate the request type and act accordingly? I.e. invoke `rest_api_server_error()` only if it's an API request _and_ JSON content was requested?
Author
Owner

@thatmattlove commented on GitHub (Sep 1, 2021):

Cool, looks good! I didn't delve deep enough to see the exception middleware, so your solution makes more sense. It like the content-type is now evaluated before the debug setting as well, so this should disappear from development environments as well (I suspect that's the reason I noticed this).

@thatmattlove commented on GitHub (Sep 1, 2021): Cool, looks good! I didn't delve deep enough to see the exception middleware, so your solution makes more sense. It like the content-type is now evaluated _before_ the debug setting as well, so this should disappear from development environments as well (I suspect that's the reason I noticed this).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5278