Skip to content

Commit

Permalink
python: Show line number when an exception occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
elro444 committed Dec 28, 2024
1 parent 52dc176 commit 9df485a
Showing 1 changed file with 9 additions and 1 deletion.
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

0 comments on commit 9df485a

Please sign in to comment.