From 87c24e5a7cd629c25f100e77b35bbebb1b9fe210 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 2 Jan 2017 11:57:51 +0900 Subject: [PATCH] Prevent flooding of meaningless traceback in `sanic_endpoint_test` When Sanic has an exception in a request middleware, it fails to save request object in `results`. In `sanic_endpoint_test`, because it always requires `results` to have both `request` and `response` objects, it prints traceback like attached example. It is not a user code and it doesn't give any information to users, it is better to suppress to print this kind of error. To fix it, this patch insert collect hook as first request middleware to guarantee to successfully run it always. ``` app = , method = 'get', uri = '/ping/', gather_request = True, loop = None debug = True, request_args = (), request_kwargs = {} _collect_request = ._collect_request at 0x11286c158> _collect_response = ._collect_response at 0x11286c378> def sanic_endpoint_test(app, method='get', uri='/', gather_request=True, loop=None, debug=False, *request_args, **request_kwargs): results = [] exceptions = [] if gather_request: @app.middleware def _collect_request(request): results.append(request) async def _collect_response(sanic, loop): try: response = await local_request(method, uri, *request_args, **request_kwargs) results.append(response) except Exception as e: exceptions.append(e) app.stop() app.run(host=HOST, debug=debug, port=42101, after_start=_collect_response, loop=loop) if exceptions: raise ValueError("Exception during request: {}".format(exceptions)) if gather_request: try: > request, response = results E ValueError: not enough values to unpack (expected 2, got 1) ../sanic/sanic/utils.py:46: ValueError ``` --- sanic/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/utils.py b/sanic/utils.py index 88444b3c..cb6d70c4 100644 --- a/sanic/utils.py +++ b/sanic/utils.py @@ -22,9 +22,9 @@ def sanic_endpoint_test(app, method='get', uri='/', gather_request=True, exceptions = [] if gather_request: - @app.middleware def _collect_request(request): results.append(request) + app.request_middleware.appendleft(_collect_request) async def _collect_response(sanic, loop): try: