title: werkzeug custom flask error handling
body: <p>I am trying to get my custom errorhandlers to invoke when raising an exception. I am a bit new  to python/flask/werkzeug. I am wrapping my api with werkzeug DispatcherMiddleware, and cant seem to invoke my custom handlers.</p>
  
  <p>manage.py</p>
  
  <pre><code>from werkzeug.serving import run_simple
  from werkzeug.wsgi import DispatcherMiddleware
  
  
  from myapp import api, web
  
  
  app = DispatcherMiddleware(web.create_app(), {
      '/api': api.create_app()
  })
  
  
  if __name__ == '__main__':
    run_simple('0.0.0.0', 5000, app, use_reloader=True, use_debugger=True)
  </code></pre>
  
  <p>api/<strong>init</strong>.py</p>
  
  <pre><code>def create_app(settings_override=None):
  
    app = Flask(__name__)
    api = Api(app)
  
    # Set default encoder
    app.json_encoder = JSONEncoder
  
    # Register custom error handlers
    app.errorhandler(BaseErrorException)(on_error)
    app.errorhandler(BaseFormException)(on_formerror)
    app.errorhandler(404)(on_404)
  
    db.init_app(app)
  
    api_urls(api)
  
    return app
  
  
  def on_error(e):
    print 'on_error'
    return jsonify(dict(error=e.msg)), 400
  
  
  def on_formerror(e):
    print 'on_formerror'
    return jsonify(dict(errors=e.errors)), 400
  
  
  def on_404(e):
    print 'on_404'
    return jsonify(dict(error='Not found')), 404
  </code></pre>
  
  <p>But I keep getting:</p>
  
  <pre><code>{
      "status": 500,
      "message": "Internal Server Error"
  }
  </code></pre>
  
  <p>console output:</p>
  
  <pre><code>Traceback (most recent call last):
  ...
  raise BaseFormException(form.errors)
  BaseFormException
  </code></pre>