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
215 views
in Technique[技术] by (71.8m points)

javascript - Catching RequestEntityTooLarge from Flask server

Surprisingly, I couldn't much info about others having the same issue.

To my understanding, whenever file exceeds MAX_CONTENT_LENGTH, it automatically throws the 413. The question is how do I catch it?

For some odd reason, I can catch 415 status codes AND custom 413 messages on the client side (haven't tested with other status codes), but I can't catch ones specifically from RequestEntityTooLarge.

I set the max size as such: app.config["MAX_CONTENT_LENGTH"] = 5242880

My Flask code:

def convert():
    try:
        data = request.form
        fileUpload = request.files["fileUpload"]
        # maybe some other code
    except RequestEntityTooLarge as e:
        print ("- File too large!")
        # I can never catch this
        return jsonify("large_file"), 413

    if file not in valid_formats:
        # this is always caught on client, even if I change this status code to 413
        # return jsonify("bad_format"), 413
        return jsonify("bad_format"), 415

Client side (vanilla JS, with Fetch API)

    fetch("/api/convert", {
        method: "POST",
        body: formData
    }).then(response => {
        console.log(response.status);
        // do whatever based on status
        // here, i can catch 415 and custom 413 messages
        return response.json();
    }).then(data => {
        // do some stuff
    }).catch(error => {
        console.log(error);
    })

In the network tab in my browser, I can see that if I send a custom 413 message (return jsonify("bad_format"), 413, it shows that it received status code 413.

custom 413 message

However, when I try to send 413 message when catching RequestEntityTooLarge, there's no status code...?

with RequestEntityToLarge

I tried it in Firefox and Chrome, with both the Flask production server, and Gunicorn server. Such odd behaviour I do not understand!


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...