Skip to content

Commit

Permalink
Use a different way of parsing/cleaning the stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Wout Feys committed Aug 21, 2024
1 parent 4f778bd commit bf16748
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 17 additions & 8 deletions aikido_firewall/helpers/get_clean_stacktrace.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
"""Exports function `get_clean_stacktrace`"""

import traceback
import inspect
import sys


def get_clean_stacktrace():
"""Returns a cleaned up stacktrace"""
stack_trace = traceback.extract_stack()
filtered_stack_trace = filter(filter_no_aikido, stack_trace)
# Get the current stack
stack = inspect.stack()

formatted_trace = "".join(traceback.format_list(filtered_stack_trace))
return formatted_trace
# List of built-in modules to filter out
ignored_modules = sys.builtin_module_names

cleaned_stack = []

def filter_no_aikido(frame):
"""Custom filter to remove aikido frames"""
return "/site-packages/aikido_firewall/" not in frame.filename
for frame_info in stack:
name = frame_info.frame.f_globals.get("__name__", "")

if name not in ignored_modules and not name.startswith("aikido_firewall"):
cleaned_stack.append(
f"File: {frame_info.filename}, L{frame_info.lineno} {frame_info.function}(...)"
)

cleaned_stack.reverse()
return "• " + "\n\n• ".join(cleaned_stack)
3 changes: 0 additions & 3 deletions aikido_firewall/helpers/get_clean_stacktrace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ def dummy_function():
return get_clean_stacktrace()

result = dummy_function()

assert "get_clean_stacktrace_test.py" in result
assert "dummy_function" in result

0 comments on commit bf16748

Please sign in to comment.