Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Show line number when an exception occurs #7264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion redash/query_runner/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib
import logging
import sys
import traceback

from RestrictedPython import compile_restricted
from RestrictedPython.Guards import (
Expand Down Expand Up @@ -360,7 +361,14 @@ def run_query(self, query, user):
self.validate_result(data)
data["log"] = self._custom_print.lines
except Exception as e:
error = str(type(e)) + " " + str(e)
_, _, tb = sys.exc_info()
frames = traceback.extract_tb(tb)
# We want the "deepest" frame that is in the query code (indicated by '<string>')
# This is to avoid exceptions raised by modules the query imported.
query_frames = [frame for frame in frames if frame.filename == "<string>"]
line_number = query_frames[-1].lineno

error = f'{type(e)} "{e}" (line {line_number})'
data = None

return data, error
Expand Down
Loading