TurboGears / tg2

Python web framework with full-stack layer implemented on top of a microframework core with support for SQL DBMS, MongoDB and Pluggable Applications
http://www.turbogears.org/
Other
806 stars 78 forks source link

Route/HTTP middleware after method should execute after exception response generate #128

Open smurf-U opened 2 years ago

smurf-U commented 2 years ago

Describe the bug

Trying to figure out how long it takes to process a route and if it is successful or not. By creating dispatch controller wrapper to calculate response time with status code and route info for example:

datas = []
def controller_wrapper(next_caller):
    route = {}
    def call(*args, **kw):
        try:
            route.update({'start': datetime.now(), 'name': request.route.url})
            print('Before handler!')
            return next_caller(*args, **kw)
        finally:
            print('After Handler!')
            route.update({'end':datetime.now(), 'status': response.get_status()})
            datas.append(route)
            print(":::::response", response.status_code, dir(response))

    return call
config.get_component('dispatch').register_controller_wrapper(controller_wrapper)

After Register above wrapper, and if one of controller gives exception then it will give status_code value as 200. Which is not accurate.

Expected behaviour

response.status_code should give 500 rather then None.

Version

turbogears2==2.4.3