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

Cryptic 500 Internal Server Error when testing ASGI app endpoint #2098

Open
berzi opened this issue Aug 27, 2022 · 15 comments
Open

Cryptic 500 Internal Server Error when testing ASGI app endpoint #2098

berzi opened this issue Aug 27, 2022 · 15 comments
Labels
documentation good first issue needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! question
Milestone

Comments

@berzi
Copy link

berzi commented Aug 27, 2022

I'm trying to test an endpoint of my ASGI app using pytest. For now I'm simply running client.simulate_post("/my/endpoint)" and doing nothing with it. The test succeeds, but regardless of what I put into the request, result.json is always {'title': '500 Internal Server Error'}.

This could very well mean that something in my endpoint is broken, which is why I want the test in the first place, but the trouble is that I get no information on what went wrong or where. I don't expect that to be in the response for obvious reasons, but even if I set a debugger breakpoint on the very first line of the endpoint's handler, it is never triggered, and looking around with the debugger on the simulate_post() call itself I couldn't figure out what is wrong either.

It would seem the error occurs before the request handler is even called, meaning something is wrong in my setup, but the simulate_post() call itself does not raise any exception or something inside it catches it and turns it into a 500 inside the response, effectively hiding the error and its cause. How could I debug this further?

@berzi berzi changed the title Cryptic 500 Internal Server Error in async test Cryptic 500 Internal Server Error when testing ASGI app endpoint Aug 27, 2022
@vytas7
Copy link
Member

vytas7 commented Aug 27, 2022

Hi @berzi!
Yeah, one of the reasons behind this behaviour is that ASGI, unlike WSGI's wsgi.errors, doesn't define any stream for logging errors back to the server.

I find it useful to configure logging, e.g.,

import logging

logging.basicConfig(
    format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO
)

You should now be able to see Falcon log the traceback in question.

@berzi
Copy link
Author

berzi commented Aug 27, 2022

Thank you for the very fast answer. That worked. Note for anyone who might have a similar issue: I also had to set pytest's log_cli = true for the logs to show up.

I think it would make sense to mention this caveat somewhere in the guide to testing on ASGI apps, not sure where though.

@vytas7
Copy link
Member

vytas7 commented Aug 27, 2022

We ought to at least add a FAQ item, and maybe link to it from the ASGI tutorial.

@vytas7 vytas7 added good first issue needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! labels Aug 27, 2022
@ryuusama09
Copy link

has this issue been assigned or solved ? . If not then i would like to help

@vytas7
Copy link
Member

vytas7 commented Sep 2, 2022

Not assigned nor solved -- you're welcome to help!

@vytas7 vytas7 removed the needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! label Sep 2, 2022
@ryuusama09
Copy link

hi , regarding this , where can i start from ?. I have brief to almost no idea about this project

@vytas7
Copy link
Member

vytas7 commented Sep 3, 2022

Then you'll need to familiarize yourself with this issue and the whole project too 🙂

Since this issue is more pertinent to the async flavour of Falcon, maybe you could take our ASGI tutorial for a spin to start with?

@techrajdeep
Copy link

I am looking for an issue for my first contribution . Can you help me here.

@vytas7
Copy link
Member

vytas7 commented Oct 10, 2022

Hi @techrajdeep!
Yes -- same as in my comment just above, maybe you could take the ASGI tutorial and/or write a simple ASGI app and a test case for it, just to get the feeling of the problem @berzi reported.

Then it is about expanding our FAQ and/or tutorials.

@vytas7 vytas7 added the needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! label Dec 16, 2023
@vytas7
Copy link
Member

vytas7 commented Dec 16, 2023

Hi again @berzi & @techrajdeep, just checking whether any of you are still interested in contributing to this issue?

@berzi
Copy link
Author

berzi commented Dec 16, 2023

At the moment, personally, no. I might if it's still unresolved if/when I start using the project more actively.

@vytas7
Copy link
Member

vytas7 commented Dec 16, 2023

Thanks for getting back @berzi. And sorry, I instead meant to highlight @ryuusama09 and @techrajdeep who expressed interest in working this 😅

@Jayaprakash-dev
Copy link

Jayaprakash-dev commented Jan 12, 2024

Hey @vytas7, @berzi,

I've looked into the codebase, specifically examining how URI routes are added and how the Falcon app searches for a route. The issue might be linked to an internal server problem, possibly encountering an unhandled exception that prevented the fulfillment of the request. Alternatively, it could be related to not passing any data in the POST request.

I've tested the code for the POST request, ensuring it results in either a '400' or '404' error.

For instance, when initiating an HTTP POST request to a resource class named Images with the URI template /images, the app responds with a '400 - bad request' error or '405 - method not allowed' if you haven't registered an on_post request handler. On the other hand, if there's no registered resource class for the specified URI template, the app returns a '404 - path not found' error.

#[Ref]

@vytas7
Copy link
Member

vytas7 commented Jan 13, 2024

Hi @Jayaprakash-dev, and thanks for looking at this.
I believe the issue here is not the 500-error itself, but inadequate documentation on how to set up logging so that the resulting tracebacks are not suppressed in this case, particularly for ASGI apps that (unlike WSGI) don't define any error stream.

So the question was whether anyone could contribute a (primarily documentation?) PR on how to better debug this.

@MRLab12
Copy link
Contributor

MRLab12 commented Apr 2, 2024

@vytas7 I'm happy to help with this. Looking though the previous comments here, seems like the issue would just be updating documentation so that users can set up logging for debugging?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation good first issue needs contributor Comment on this issue if you'd like to volunteer to work on this. Thanks! question
Projects
None yet
Development

No branches or pull requests

6 participants