I am creating a server with Flask and it has quite a few routes. Most routes require a specific state, such as "logged in" or "process started". All of my functions and methods in packages will throw certain type of exceptions when the specific state is not matched. At the end my code ends up like this:
@app.route("/sayHello") def sayHello(): try: name = get_session_data("user", "name") # Some other stuff that throw meaningful exceptions return someTemplate except NotLoggedIn: return flask.redirect(flask.url_for("login")) except UserNotReadyToSayHello: return "You are not ready to say hello!", status.HTTP_400_BAD_REQUEST except UserHelloMessageNotDefined: return "You did not set up a greeting message!", status.HTTP_400_BAD_REQUEST except ConnectionToDatabaseFailed: return "Cannot access database", status.HTTP_500_INTERNAL_SERVER_ERROR except UnexpectedError: return "Something happened", status.HTTP_500_INTERNAL_SERVER_ERROR
Of course this is just an example I made up to demonstrate the problem, however it is more or less equivalent to the actual code.
This made programming the routes much easier than checking everything with
if statements everywhere, however I ended up with a lot of
except cases to the point where PyLint is telling me
Too many return statements (9/6)
How should I handle this situation? A lot of routes actually share a very similar sets of