Skip to content

Commit b6b740a

Browse files
author
Hanzhang Zeng (Roger)
authored
Add marshall exception trace (#482)
1 parent 719eb33 commit b6b740a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

azure/functions_worker/dispatcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import logging
99
import queue
1010
import threading
11-
import traceback
1211
import os
1312
import sys
1413

@@ -22,6 +21,7 @@
2221
from . import constants
2322

2423
from .logging import error_logger, logger
24+
from .tracing import marshall_exception_trace
2525

2626

2727
class DispatcherMeta(type):
@@ -188,7 +188,7 @@ def _serialize_exception(self, exc):
188188
f'Could not serialize original exception message.')
189189

190190
try:
191-
stack_trace = ''.join(traceback.format_tb(exc.__traceback__))
191+
stack_trace = marshall_exception_trace(exc)
192192
except Exception:
193193
stack_trace = ''
194194

azure/functions_worker/tracing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
import traceback
3+
4+
5+
def marshall_exception_trace(exc: Exception) -> str:
6+
stack_summary: traceback.StackSummary = traceback.extract_tb(
7+
exc.__traceback__)
8+
if isinstance(exc, ModuleNotFoundError):
9+
stack_summary = _marshall_module_not_found_error(stack_summary)
10+
return ''.join(stack_summary.format())
11+
12+
13+
def _marshall_module_not_found_error(
14+
tbss: traceback.StackSummary
15+
) -> traceback.StackSummary:
16+
tbss = _remove_frame_from_stack(tbss, '<frozen importlib._bootstrap>')
17+
tbss = _remove_frame_from_stack(
18+
tbss, '<frozen importlib._bootstrap_external>')
19+
return tbss
20+
21+
22+
def _remove_frame_from_stack(
23+
tbss: traceback.StackSummary,
24+
framename: str
25+
) -> traceback.StackSummary:
26+
filtered_stack_list: List[traceback.FrameSummary] = list(
27+
filter(lambda frame: getattr(frame, 'filename') != framename, tbss))
28+
filtered_stack: traceback.StackSummary = traceback.StackSummary.from_list(
29+
filtered_stack_list)
30+
return filtered_stack

tests/test_broken_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ async def test_load_broken__import_error(self):
134134

135135
self.assertIn('ImportError',
136136
r.response.result.exception.message)
137+
self.assertNotIn('<frozen importlib._bootstrap>',
138+
r.response.result.exception.message)
139+
self.assertNotIn('<frozen importlib._bootstrap_external>',
140+
r.response.result.exception.message)
137141

138142
async def test_load_broken__inout_param(self):
139143
async with testutils.start_mockhost(

0 commit comments

Comments
 (0)