Skip to content

2308: Return server errors in json for API - #4487

Open
bahaaTuffaha wants to merge 1 commit into
developfrom
2308-Return-server-errors-in-json-for-API
Open

2308: Return server errors in json for API#4487
bahaaTuffaha wants to merge 1 commit into
developfrom
2308-Return-server-errors-in-json-for-API

Conversation

@bahaaTuffaha

@bahaaTuffaha bahaaTuffaha commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Short description

The API should always return JSON, even if an error occurs.

Proposed changes

  • Added an is_api_request helper that checks request.resolver_match.app_name against "api", falling back to a request.path prefix check when the URL couldn't be resolved.
  • Added a render_error_response function that returns a JsonResponse for API requests and otherwise falls back to HttpResponse wrapping the existing render_error_template.
  • Updated handler400, handler403, handler404, handler500, and csrf_failure to use render_error_response instead of constructing the response classes directly.
  • Changed the return type for the handlers from HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotFound, and HttpResponseServerError to the generic HttpResponse.

Side effects

None.

Faithfulness to issue description and design

There are no intended deviations from the issue and design.

How to test

  • Launch the server in non debug mode I used this command (Debug=0):
source .venv/bin/activate

DJANGO_SETTINGS_MODULE=integreat_cms.core.docker_settings \
INTEGREAT_CMS_DEBUG=0 \
INTEGREAT_CMS_SECRET_KEY=dummy \
DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib \
integreat-cms-cli runserver --insecure localhost:8000

Note: DYLD_FALLBACK_LIBRARY_PATH is a macOS/Homebrew workaround for cairocffi skip it on Linux.

Resolved issues

Fixes: #2308


Pull Request Review Guidelines

@MizukiTemma MizukiTemma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you 😸

@MizukiTemma MizukiTemma self-assigned this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server errors in API return HTML response instead of JSON

2 participants