Shangway Hsu
Shangway Hsu

Reputation: 21

Flask cannot raise HTTP exception after try catching Runtime error

When I try to raise a HTTP exception status code 400 it only prints the json error message on the browser but does not state HTTP/1.1 400 BAD REQUEST in the console like it is supposed to. The exception raising works for all other parts of my program but it doesn't work when I do it in a try-catch for a runtime error.

My exception handler is exactly this: http://flask.pocoo.org/docs/0.11/patterns/apierrors/

my try-catch:

try:
    // run some program
catch RuntimeError as e:
    raise InvalidUsage(e.message, status_code=400)

Upvotes: 2

Views: 3171

Answers (1)

reptilicus
reptilicus

Reputation: 10397

You should use the abort function of flask, something like:

from flask import abort

@app.route("/some_route")
def some_route():
    try:
        # do something
    except SomeException:
         abort(400, "Some message")

Upvotes: 3

Related Questions