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

Add documentation for debug error handling #924

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Lunrtick
Copy link

This is a first attempt at documenting the error handling with DEBUG on or off, as discussed in #619

@vitalik
Copy link
Owner

vitalik commented Nov 15, 2023

@Lunrtick thank you

I was actually thinking to add extra check - do not shallow exception if ATOMIC_REQUESTS is set (which I think is not for 95% users)

@Lunrtick
Copy link
Author

That could definitely work. It would also mean that the user wouldn't get a stack trace back.

They could of course look in the terminal to see the error, so I suppose it doesn't matter too much either way.

Thinking about it now, maybe a Django middleware that comes as an optional extra with django-ninja would be the right way to go. You could keep lines 107 and 108 here and remove lines 109-112.

def _default_exception(
request: HttpRequest, exc: Exception, api: "NinjaAPI"
) -> HttpResponse:
if not settings.DEBUG:
raise exc # let django deal with it
logger.exception(exc)
tb = traceback.format_exc()
return HttpResponse(tb, status=500, content_type="text/plain")

Instead, the logic could be

if not settings.DEBUG: 
    raise exc  # let django deal with it
else:
    raise NinjaAPIException() from exc 

Or even just

raise NinjaAPIException() from exc 

and then the middleware would be something simple like

# catch NinjaAPIException
if settings.DEBUG:
    return 500, {"a nice": "stack trace"}
else:
    raise exc

This would mean that ATOMIC_REQUESTS should work as expected either way.

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

Successfully merging this pull request may close these issues.

None yet

2 participants