how to use exception_handler in an async view function

Author: song61069140Created Jun 4, 2026Updated Jul 13, 2026

use @api.exception_handler(Exception) to catch exception in an async view function.

python
@api.exception_handler(Exception)
async def service_unavailable(request: HttpRequest, exc):

    try:
        raise exc
    except(BaseException,):
        s_err_info = traceback.format_exc()

        
    return api.create_response(request, {"c": -1, "m": "error"}, status=200)

django bug report:

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