Return server error messages as JSON when handling REST API requests #4241

Closed
opened 2025-12-29 18:34:11 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Nov 3, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.6.9
  • NetBox version: 2.9.8

Proposed Functionality

When a REST API request triggers an unhandled exception, the response should convey the error message and any other pertinent information as JSON-formatted data.

Use Case

When an unhandled exception occurs during the processing of a request (and debugging is disabled), NetBox returns a human-friendly HTML page citing the error message and other information. While helpful to humans, this content is not appropriate in a REST API response, which should contain only JSON data. For example:

{
    "exception": "ValueError",
    "error_message": "Invalid something something",
    "python_version": "3.6.9",
    "netbox_version": "2.9.8"
}

The is_api_request() utility function may be helpful for discerning REST API requests from web UI requests.

Database Changes

None

External Dependencies

None

Originally created by @jeremystretch on GitHub (Nov 3, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.6.9 * NetBox version: 2.9.8 ### Proposed Functionality When a REST API request triggers an unhandled exception, the response should convey the error message and any other pertinent information as JSON-formatted data. ### Use Case When an unhandled exception occurs during the processing of a request (and debugging is disabled), NetBox returns a human-friendly HTML page citing the error message and other information. While helpful to humans, this content is not appropriate in a REST API response, which should contain only JSON data. For example: ```json { "exception": "ValueError", "error_message": "Invalid something something", "python_version": "3.6.9", "netbox_version": "2.9.8" } ``` The `is_api_request()` utility function may be helpful for discerning REST API requests from web UI requests. ### Database Changes None ### External Dependencies None
adam added the status: acceptedtype: feature labels 2025-12-29 18:34:11 +01:00
adam closed this issue 2025-12-29 18:34:11 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4241