title: Handle all exceptions in Flask route
body: <p>I have a simple route in my Flask app:</p>
  
  <pre><code>@app.route('/', methods=['GET'])
  def homepage():
      return '{}'.format(1 / 0)
  </code></pre>
  
  <p>When the user visits site.com/ she should see the result of 1/0. Of course that's not possible, so Python throws me an error.</p>
  
  <p>Now, I would like all errors across all my endpoints to be handled in a special way: I would like them to return a JSON response, like:</p>
  
  <pre><code>{
      'status_code': 500,
      'status': 'Internal Server Error'
  }
  </code></pre>
  
  <p>I wrote a decorator to do exactly this:</p>
  
  <pre><code>def return_500_if_errors(f):
      def wrapper(*args, **kwargs):
          try:
              return f(*args, **kwargs)
          except:
              response = {
                  'status_code': 500,
                  'status': 'Internal Server Error'
              }
              return flask.jsonify(response), 500
      return wrapper
  </code></pre>
  
  <p>I then added the decorator to the endpoint:</p>
  
  <pre><code>@return_500_if_errors
  @app.route('/', methods=['GET'])
  def homepage():
      return '{}'.format.(1 / 0)
  </code></pre>
  
  <p>However, the decorator seems to have no effect. </p>