From c6d9191cad2f3a331ddb8f1650ea9f0894017750 Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Wed, 1 Feb 2023 19:01:45 -0600 Subject: [PATCH 1/3] =?UTF-8?q?#886=EF=BC=9A=20Error=20raised=20from=20Fla?= =?UTF-8?q?sk=20app=20is=20logged=20as=20successful=20function=20execution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- azure/functions/_http_wsgi.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/azure/functions/_http_wsgi.py b/azure/functions/_http_wsgi.py index d108bc54..7e31264d 100644 --- a/azure/functions/_http_wsgi.py +++ b/azure/functions/_http_wsgi.py @@ -1,6 +1,5 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. - from typing import Dict, List, Optional, Any import logging from io import BytesIO, StringIO @@ -192,13 +191,16 @@ def _handle(self, req, context): wsgi_request = WsgiRequest(req, context) environ = wsgi_request.to_environ(self._wsgi_error_buffer) wsgi_response = WsgiResponse.from_app(self._app, environ) - self._handle_errors() + self._handle_errors(wsgi_response) return wsgi_response.to_func_response() - def _handle_errors(self): + def _handle_errors(self, wsgi_response): if self._wsgi_error_buffer.tell() > 0: self._wsgi_error_buffer.seek(0) error_message = linesep.join( self._wsgi_error_buffer.readline() ) raise Exception(error_message) + + if wsgi_response._status_code == 500: + raise Exception(b''.join(wsgi_response._buffer)) From 899500b03dbeaa5cb62dff696b0c6a04738d6110 Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Mon, 27 Feb 2023 21:25:31 +0000 Subject: [PATCH 2/3] change wsgi status code to >= 500 --- azure/functions/_http_wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure/functions/_http_wsgi.py b/azure/functions/_http_wsgi.py index 7e31264d..fdba6d95 100644 --- a/azure/functions/_http_wsgi.py +++ b/azure/functions/_http_wsgi.py @@ -202,5 +202,5 @@ def _handle_errors(self, wsgi_response): ) raise Exception(error_message) - if wsgi_response._status_code == 500: + if wsgi_response._status_code >= 500: raise Exception(b''.join(wsgi_response._buffer)) From c8bce6e92140ea4c51aa48c50c7c0ec1c843407b Mon Sep 17 00:00:00 2001 From: peterstone2017 <12449837+YunchuWang@users.noreply.github.com> Date: Thu, 2 Mar 2023 14:29:38 -0600 Subject: [PATCH 3/3] add test --- tests/test_http_wsgi.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_http_wsgi.py b/tests/test_http_wsgi.py index 5723dfa4..701fc68e 100644 --- a/tests/test_http_wsgi.py +++ b/tests/test_http_wsgi.py @@ -4,6 +4,8 @@ import unittest from io import StringIO, BytesIO +import pytest + import azure.functions as func from azure.functions._abc import TraceContext, RetryContext from azure.functions._http import HttpResponseHeaders @@ -181,6 +183,20 @@ def main(req, context): self.assertEqual(func_response.status_code, 200) self.assertEqual(func_response.get_body(), b'sample string') + def test_middleware_handle_with_server_error_status_code(self): + """Test if the middleware can be used by exposing the .handle method, + specifically when the middleware is used as + def main(req, context): + return WsgiMiddleware(app).handle(req, context) + """ + app = self._generate_wsgi_app(status="500 Internal Server Error", + response_body=b'internal server error') + func_request = self._generate_func_request() + with pytest.raises(Exception) as exec_info: + func_response = WsgiMiddleware(app).handle(func_request) + self.assertEqual(func_response.status_code, 500) + self.assertEqual(exec_info.value.args[0], b'internal server error') + def test_middleware_wrapper(self): """Test if the middleware can be used by exposing the .main property, specifically when the middleware is used as