connexion icon indicating copy to clipboard operation
connexion copied to clipboard

connexion.problem not compatible with add_error_handler

Open mxmo0rhuhn opened this issue 9 years ago • 6 comments

Description

Returning an instance of connexion.problem from an error handler results in an application crash in connexion == 1.1.9

Expected behaviour (connexion == 1.0.129)

Returning a nice error like:

HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: application/problem+json
Content-Length: 129
Server: Werkzeug/0.12.2 Python/3.6.1

{
  "type": "about:blank",
  "title": "Internal Server Error",
  "detail": "Could not connect to LDAP server",
  "status": 500
}

Actual behaviour in (connexion == 1.1.9)

Stacktrace

Traceback (most recent call last):
  File "/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "/lib/python3.6/site-packages/flask/app.py", line 1740, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/lib/python3.6/site-packages/werkzeug/wrappers.py", line 885, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/lib/python3.6/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/lib/python3.6/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'ConnexionResponse' object is not callable

Steps to reproduce

  1. Define handler:
def ldap_connection_error(the_error):
    return connexion.problem(status=500, title=http.client.responses[500], detail='Could not connect to LDAP server')
  1. Add the handler to the app:
    app = connexion.App(__name__, specification_dir='./swagger/')
    app.add_error_handler(LDAPSocketOpenError, error_handlers.ldap_connection_error) 
  1. Raise the corresponding exception and the app will crash.

Additional info:

Output of the commands:

  • python --version Python 3.6.1
  • pip show connexion | grep "^Version\:" Version: 1.0.129 / Version: 1.1.9

mxmo0rhuhn avatar Sep 10 '17 13:09 mxmo0rhuhn

Thanks for reporting this. Would you be willing to work on a fix?

hjacobs avatar Sep 12 '17 05:09 hjacobs

Sadly I currently got no capacity to work on a fix. :/

mxmo0rhuhn avatar Sep 13 '17 00:09 mxmo0rhuhn

@hjacobs Hi. I want to log response validation errors into Sentry. Is it possible to do this with add_error_handler ? I've tried to do app.add_error_handler(Exception, sentry_handler) but it never got called. How do I approach this if I want to fix\implement this?

pkeeper avatar Oct 11 '17 14:10 pkeeper

This seems to be because ConnexionResponse (created by connexion.problem) is not callable once you've passed all the args to it.

cetanu avatar Mar 13 '18 23:03 cetanu

Werkzeug BaseResponse class implements __call__ which is not implemented in ConnexionResponse

From werkzeug:

    def __call__(self, environ, start_response):
        """Process this response as WSGI application.

        :param environ: the WSGI environment.
        :param start_response: the response callable provided by the WSGI
                               server.
        :return: an application iterator
        """
        app_iter, status, headers = self.get_wsgi_response(environ)
        start_response(status, headers)
        return app_iter

Doesn't work also when using flask.make_response, as it ends up in the same code path.

mohabusama avatar Mar 14 '18 10:03 mohabusama

Digging into connexion code, found this method: https://github.com/zalando/connexion/blob/master/connexion/apps/flask_app.py#L38

So FlaskApi.get_response() is your friend here :)

mohabusama avatar Mar 14 '18 10:03 mohabusama