8000 Add crash header to 500 responses (#114) · jrmfg/functions-framework-python@168617c · GitHub
[go: up one dir, main page]

Skip to content

Commit 168617c

Browse files
authored
Add crash header to 500 responses (GoogleCloudPlatform#114)
1 parent 348064d commit 168617c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/functions_framework/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
DEFAULT_SIGNATURE_TYPE = "http"
4141
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
4242

43+
_FUNCTION_STATUS_HEADER_FIELD = "X-Google-Status"
44+
_CRASH = "crash"
45+
4346

4447
class _Event(object):
4548
"""Event passed to background functions."""
@@ -160,6 +163,13 @@ def read_request(response):
160163
return response
161164

162165

166+
def crash_handler(e):
167+
"""
168+
Return crash header to allow logging 'crash' message in logs.
169+
"""
170+
return str(e), 500, {_FUNCTION_STATUS_HEADER_FIELD: _CRASH}
171+
172+
163173
def create_app(target=None, source=None, signature_type=None):
164174
# Get the configured function target
165175
target = target or os.environ.get("FUNCTION_TARGET", "")
@@ -215,6 +225,7 @@ def create_app(target=None, source=None, signature_type=None):
215225
# 5. Create the application
216226
app = flask.Flask(target, template_folder=template_folder)
217227
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
228+
app.register_error_handler(500, crash_handler)
218229
global errorhandler
219230
errorhandler = app.errorhandler
220231

0 commit comments

Comments
 (0)
0