Skip to content
Merged
Changes from 3 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
47 changes: 46 additions & 1 deletion google/cloud/dataproc_spark_connect/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ class DataprocSparkConnectException(Exception):
def __init__(self, message):
self.message = message
super().__init__(message)
self._setup_ipython_exception_handler()
Comment thread
fangyh20 marked this conversation as resolved.
Outdated

def _render_traceback_(self):
return self.message
return [self.message]
Comment thread
fangyh20 marked this conversation as resolved.
Comment thread
medb marked this conversation as resolved.

def _setup_ipython_exception_handler(self):
"""Setup custom exception handler for IPython environments to ensure minimal traceback display."""
try:
from IPython import get_ipython
import sys
Comment thread
fangyh20 marked this conversation as resolved.
Outdated

ipython = get_ipython()
if ipython is not None:
Comment thread
fangyh20 marked this conversation as resolved.
Outdated
# Store original method if not already stored
if not hasattr(ipython, "_original_showtraceback"):
ipython._original_showtraceback = ipython.showtraceback

def custom_showtraceback(
exc_tuple=None,
filename=None,
tb_offset=None,
exception_only=False,
running_compiled_code=False,
):
# Get the current exception info
_, value, _ = (
sys.exc_info() if exc_tuple is None else exc_tuple
)

# If it's our custom exception, show only the message
if isinstance(value, DataprocSparkConnectException):
print(f"Error: {value.message}", file=sys.stderr)
else:
# Use original behavior for other exceptions
ipython._original_showtraceback(
exc_tuple,
filename,
tb_offset,
exception_only,
running_compiled_code,
)
Comment thread
fangyh20 marked this conversation as resolved.
Outdated

# Override the method
ipython.showtraceback = custom_showtraceback

except ImportError:
Comment thread
fangyh20 marked this conversation as resolved.
Outdated
# Not in IPython environment, no action needed
pass