Flask app raises a 500 error with no exception

44,868

After beating my head against this some more I finally figured it out thanks to the awesome people on the pocoo google group (I have since learned that there is a separate list for flask). Firstly, I needed to turn on the PROPAGATE_EXCEPTIONS option in my app configuration (http://flask.pocoo.org/docs/config/#builtin-configuration-values).

After that was done I realized there was an issue with not returning a response from a view function, which Flask interpreted this method as. Since that was the case, this issue was resolved by just adding:

return jsonify(result={"status": 200})

To the end of the try block. I hope this helps someone in a similar situation in the future.

Share:
44,868
Slater Victoroff
Author by

Slater Victoroff

CTO and cofounder of indico. Trying to make machine learning accessible to every developer.

Updated on January 11, 2020

Comments

  • Slater Victoroff
    Slater Victoroff over 4 years

    I've been banging my head against this method in Flask for some time, and while it seems I'm making progress now, I've just happened upon something that baffles me to no end. Here is the method I'm calling:

    @app.route('/facedata/<slug>', methods=["POST"])
    def facedata(slug):
        if request.method == "POST":
            try:
                post = Post.objects.get_or_404(slug=slug)
                data = [float(item) for item in request.form.getlist('emotions[]')]
                post.face_data.append(data)
                post.save()
            except:
                traceback.print_exc(file=sys.stdout)
    

    For a long time I was getting errors in here that would then be caught in the heroku logs. Currently there are no errors, implying that it doesn't reach the except loop, but even worse, there are still 500 errors. Specifically the 500 errors I get are:

    heroku[router]: at=info method=POST path=/facedata/StripedVuitton host=cryptic-mountain-6390.herokuapp.com fwd="18.111.90.180" dyno=web.2 connect=4ms service=39ms status=500 bytes=291
    

    I'm sending these POST requests via AJAX in this method:

    var slug = document.getElementById("hidden-slug").getAttribute("value");
    data = {emotions: lRes};
    $.ajax({
        type: "POST",
        data: data,
        url: document.location.origin + "/facedata/" + slug,
        success: function(){
            console.log("Success!");
        }
    });
    

    Quite honestly I just don't know how to continue debugging this problem. It doesn't make a lot of sense to me to be getting a traceback without an exception, but maybe I'm just being naive.

    I'm using mongoengine on top of MongoHQ on Heroku if that's relevant.

  • Aphex
    Aphex over 10 years
    Thanks, this was hard to find. Setting app.debug = True takes care of PROPAGATE_EXCEPTIONS and loads of other niceties that help debug flask apps.
  • jeromej
    jeromej about 9 years
    Wouldn't it be the same to return ""?
  • Slater Victoroff
    Slater Victoroff about 9 years
    @JeromeJ I'm not sure about the default metadata that flask wraps around it's responses, but I think it's better to be explicit here.
  • brandones
    brandones over 8 years
    I had this same problem, however in my case, PROPAGATE_EXCEPTIONS was on, and the exceptions were being swallowed anyway. I was just getting 500 errors with no relevant logs. The problem ended up being caused by werkzeug.debug.DebuggedApplication. I was using it in the usual way in init.py: app = DebuggedApplication(app, evalex=True). Not sure why it caused the problem, but removing its use allowed exceptions thrown by AJAX POST requests to propagate again.