Flask app raises a 500 error with no exception

Question:

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.

Asked By: Slater Victoroff

||

Answers:

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.

Answered By: Slater Victoroff

I had the same issue, but the underlying cause was different than the one in Slater’s response:

I was muting the logger that the stack trace goes to. Be sure that you are not filtering out the flask.app logger, which is where the exception stack traces go to (note that this is different than the flask server’s informational logs, named app.api).

Answered By: Vito
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.