Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Async Error Handlers Return Unawaited Coroutines Instead of HttpResponse in Django Ninja #1154

Open
mikaelsoudev opened this issue May 7, 2024 · 0 comments

Comments

@mikaelsoudev
Copy link

mikaelsoudev commented May 7, 2024

Bug Description

When defining synchronous error handlers in Django Ninja and attempting to access Django's ORM with synchronous operations, I encounter an error indicating that I am running the ORM in an asynchronous context. To address this, I decided to define the error handler as asynchronous so I could use Django's asynchronous ORM methods. However, this change led to a new issue:

The view ninja_extra.operation.method.__call__ didn't return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an 'await' into your view.

This suggests that the error handler, although defined as asynchronous, is being called from an asynchronous context but expects a synchronous function, as it does not properly await an asynchronous function. This issue highlights a potential inconsistency in how Django Ninja handles error contexts between synchronous and asynchronous operations.

Code to Reproduce

@api.exception_handler(HttpError)
async def async_error_handler(request, exc):
    return api.create_response(request, {"detail": "error...", status=500)

Versions:

  • Python version: 3.12.2
  • Django version: 5.0.3
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant