From b88c503524f02ab02f57cfce236316a7d6e72207 Mon Sep 17 00:00:00 2001 From: asriniva <69502+asriniva@users.noreply.github.com> Date: Thu, 18 Feb 2021 15:53:08 -0800 Subject: [PATCH] Add crash header to 500 responses --- src/functions_framework/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/functions_framework/__init__.py b/src/functions_framework/__init__.py index 1e87fdb4..c44773f3 100644 --- a/src/functions_framework/__init__.py +++ b/src/functions_framework/__init__.py @@ -40,6 +40,9 @@ DEFAULT_SIGNATURE_TYPE = "http" MAX_CONTENT_LENGTH = 10 * 1024 * 1024 +_FUNCTION_STATUS_HEADER_FIELD = "X-Google-Status" +_CRASH = "crash" + class _Event(object): """Event passed to background functions.""" @@ -160,6 +163,13 @@ def read_request(response): return response +def crash_handler(e): + """ + Return crash header to allow logging 'crash' message in logs. + """ + return str(e), 500, {_FUNCTION_STATUS_HEADER_FIELD: _CRASH} + + def create_app(target=None, source=None, signature_type=None): # Get the configured function target target = target or os.environ.get("FUNCTION_TARGET", "") @@ -215,6 +225,7 @@ def create_app(target=None, source=None, signature_type=None): # 5. Create the application app = flask.Flask(target, template_folder=template_folder) app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH + app.register_error_handler(500, crash_handler) global errorhandler errorhandler = app.errorhandler