title: How to return Flask-SqlAlchemy error details
body: <p>I'm using <a href="http://flask.pocoo.org" rel="nofollow noreferrer">Flask 1.0</a>, <a href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/" rel="nofollow noreferrer">Flask-SqlAlchemy 2</a> and <a href="https://angular.io/" rel="nofollow noreferrer">Angular 7</a>.</p>
  
  <p>When SqlAlchemy throws an error I want to show a tailored error message in the frontend.</p>
  
  <p>There is a section in the <a href="http://flask.pocoo.org/docs/1.0/errorhandling/" rel="nofollow noreferrer">official Flask documentation</a> about how to handle errors and also a <a href="https://stackoverflow.com/questions/29547712/how-to-return-already-exists-error-in-flask-restless">similar question</a> here on SO, that is related to <a href="https://flask-restless.readthedocs.io/en/stable/" rel="nofollow noreferrer">Flask-Restless</a> though. And yet I'm not able to connect the dots.</p>
  
  <p>When SqlAlchemy throws an error, it looks something like this:</p>
  
  <pre><code>DETAIL:  Key (id)=(123) is not present in table "foo".
  </code></pre>
  
  <p>I'm returning the error to the route:</p>
  
  <pre><code>try:
      db.session.commit()
  except Exception as error:
      db.session.flush()
      db.session.rollback()
      return error
  </code></pre>
  
  <p>In the route I'm checking if it is an error:</p>
  
  <pre><code>if status == True:
      return jsonify( { "success": True } ), 201
  else:
      return error_response(500, str(status))
  </code></pre>
  
  <p>And my error_response class looks like this:</p>
  
  <pre><code>def error_response(status_code, message=None):
      payload = {"error": HTTP_STATUS_CODES.get(status_code, "Unknown error")}
      if message:
          payload["message"] = message
          response = jsonify(payload)
          response.status_code = status_code
      return response
  </code></pre>
  
  <p>But the response json just contains a generic error message:</p>
  
  <pre><code>"message": "Http failure response for http://127.0.0.1:5000/database/add_foo: 0 Unknown Error"
  </code></pre>