itholic commented on code in PR #45377:
URL: https://github.com/apache/spark/pull/45377#discussion_r1511983720
##########
python/pyspark/errors/utils.py:
##########
@@ -119,3 +127,73 @@ def get_message_template(self, error_class: str) -> str:
message_template = main_message_template + " " +
sub_message_template
return message_template
+
+
+def is_builtin_exception(e: BaseException) -> bool:
+ """
+ Check if the given exception is a builtin exception or not
+ """
+ builtin_exceptions = [
+ exc
+ for name, exc in vars(builtins).items()
+ if isinstance(exc, type) and issubclass(exc, BaseException)
+ ]
+ return isinstance(e, tuple(builtin_exceptions))
+
+
+def add_error_context(func: Callable[..., Any]) -> Callable[..., Any]:
+ """
+ A decorator that captures PySpark exceptions occurring during the function
execution,
+ and adds user code location information to the exception message.
+ """
+
+ @functools.wraps(func)
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
+ try:
+ return func(*args, **kwargs)
+ except Exception as e:
+ from pyspark.errors import PySparkException
+ from pyspark.errors.exceptions.captured import CapturedException
+
+ inspect_stack = inspect.stack()
+ # Stack location is different when Python running on IPython (e.g.
Jupyter Notebook)
+ user_code_space = inspect_stack[-1] if get_ipython() is None else
inspect_stack[1]
Review Comment:
This PR also covers the errors generated from Notebook. For example:
<img width="1204" alt="Screenshot 2024-03-04 at 3 41 28 PM"
src="https://github.com/apache/spark/assets/44108233/a713d8a9-097b-455f-b3b4-c5d18185b25f">
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]