Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

python - Flask app raises a 500 error with no exception

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...