From 8034a812387d75131ae6f24fe3067b4d6d883aed Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 8 Mar 2022 23:06:48 -0500 Subject: [PATCH 1/5] Clearer error raised when function gives improper return --- adafruit_wsgi/wsgi_app.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/adafruit_wsgi/wsgi_app.py b/adafruit_wsgi/wsgi_app.py index 249fbdf..05a96a1 100644 --- a/adafruit_wsgi/wsgi_app.py +++ b/adafruit_wsgi/wsgi_app.py @@ -66,7 +66,14 @@ def __call__(self, environ: Dict[str, str], start_response: Callable): if match: args, route = match - status, headers, resp_data = route["func"](request, *args) + try: + status, headers, resp_data = route["func"](request, *args) + except TypeError as err: + raise RuntimeError( + "Proper HTTP response return not given for request handler '{}'".format( + route["func"].__name__ + ) + ) from err start_response(status, headers) return resp_data From 7a50d4449cd9a141fa8203deb9ad6d717f7c97a0 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 11 Mar 2022 11:43:29 -0500 Subject: [PATCH 2/5] Rework how return is parsed Explicitly check for list/tuple of length 3 --- adafruit_wsgi/wsgi_app.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/adafruit_wsgi/wsgi_app.py b/adafruit_wsgi/wsgi_app.py index 05a96a1..b6e92e4 100644 --- a/adafruit_wsgi/wsgi_app.py +++ b/adafruit_wsgi/wsgi_app.py @@ -66,15 +66,20 @@ def __call__(self, environ: Dict[str, str], start_response: Callable): if match: args, route = match - try: - status, headers, resp_data = route["func"](request, *args) - except TypeError as err: + response_params = route["func"](request, *args) + if ( + not ( + isinstance(response_params, list) + or isinstance(response_params, tuple) + ) + or len(response_params) != 3 + ): raise RuntimeError( "Proper HTTP response return not given for request handler '{}'".format( route["func"].__name__ ) - ) from err - + ) + status, headers, resp_data = response_params start_response(status, headers) return resp_data From 0bd806ef149639dc3fa896f702b76d70f185cc39 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 11 Mar 2022 11:55:42 -0500 Subject: [PATCH 3/5] Merge isinstance calls --- adafruit_wsgi/wsgi_app.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/adafruit_wsgi/wsgi_app.py b/adafruit_wsgi/wsgi_app.py index b6e92e4..e213a95 100644 --- a/adafruit_wsgi/wsgi_app.py +++ b/adafruit_wsgi/wsgi_app.py @@ -68,10 +68,7 @@ def __call__(self, environ: Dict[str, str], start_response: Callable): args, route = match response_params = route["func"](request, *args) if ( - not ( - isinstance(response_params, list) - or isinstance(response_params, tuple) - ) + not isinstance(response_params, (list, tuple)) or len(response_params) != 3 ): raise RuntimeError( From a317a996688d733920579d8318ec3dbf26bc597f Mon Sep 17 00:00:00 2001 From: tekktrik <89490472+tekktrik@users.noreply.github.com> Date: Tue, 15 Mar 2022 21:44:47 -0400 Subject: [PATCH 4/5] Change to catch ValueError and TypeError Co-authored-by: Dan Halbert --- adafruit_wsgi/wsgi_app.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/adafruit_wsgi/wsgi_app.py b/adafruit_wsgi/wsgi_app.py index e213a95..c1cc67c 100644 --- a/adafruit_wsgi/wsgi_app.py +++ b/adafruit_wsgi/wsgi_app.py @@ -66,17 +66,12 @@ def __call__(self, environ: Dict[str, str], start_response: Callable): if match: args, route = match - response_params = route["func"](request, *args) - if ( - not isinstance(response_params, (list, tuple)) - or len(response_params) != 3 - ): + try: + status, headers, resp_data = route["func"](request, *args) + except ValueError, TypeError: raise RuntimeError( "Proper HTTP response return not given for request handler '{}'".format( route["func"].__name__ - ) - ) - status, headers, resp_data = response_params start_response(status, headers) return resp_data From b92d562bfeacf07db3f76a327a18c5e224b10876 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 15 Mar 2022 21:58:18 -0400 Subject: [PATCH 5/5] Linted and reformatted --- adafruit_wsgi/wsgi_app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adafruit_wsgi/wsgi_app.py b/adafruit_wsgi/wsgi_app.py index c1cc67c..a9e78ae 100644 --- a/adafruit_wsgi/wsgi_app.py +++ b/adafruit_wsgi/wsgi_app.py @@ -68,10 +68,12 @@ def __call__(self, environ: Dict[str, str], start_response: Callable): args, route = match try: status, headers, resp_data = route["func"](request, *args) - except ValueError, TypeError: + except (ValueError, TypeError) as err: raise RuntimeError( "Proper HTTP response return not given for request handler '{}'".format( route["func"].__name__ + ) + ) from err start_response(status, headers) return resp_data