title: Can't catch _mysql_exceptions.OperationalError in Flask app
body: <p>I have a Flask app with a route that looks like this:</p>
  
  <pre><code>from _mysql_exceptions import OperationalError
  
  @mod.route('/', methods=['POST'])
  def do_something_route():
      try:
          do_something(request.form)
      except OperationalError:
          message = {"error": "something went wrong"}
          return jsonify(message, mimetype='application/json'), 400
  </code></pre>
  
  <p>The <code>do_something()</code> function:</p>
  
  <pre><code>def do_something(self, form_data):
      model = MyModel()
      for key in form_data:
          if hasattr(model, key):
              setattr(model, key, form_data.get(key))
      db.session.add(model)
      db.session.commit()
      return model
  </code></pre>
  
  <p>When I send garbage data to <code>do_something_route</code> that violates my DB constraints, an <code>OperationalError</code> exception is raised and a response with a status code of 500 and the stack trace is returned, not my jsonified message with a status of 400.</p>
  
  <p>When I try this is a plain old python shell, it seems to work as expected:</p>
  
  <pre><code>&gt;&gt;&gt; from _mysql_exceptions import OperationalError
  &gt;&gt;&gt; raise OperationalError
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  _mysql_exceptions.OperationalError
  
  &gt;&gt;&gt; try:
  ...     raise OperationalError
  ... except OperationalError:
  ...     print 'test'
  ...
  test
  </code></pre>
  
  <p>Why is <code>except OperationalError:</code> not catching <code>OperationalError</code> exceptions? Is Flask doing some black magic that is returning a response early?</p>