HyukjinKwon commented on code in PR #39591:
URL: https://github.com/apache/spark/pull/39591#discussion_r1070884205
##########
python/pyspark/errors/exceptions.py:
##########
@@ -74,3 +79,187 @@ def getMessageParameters(self) -> Optional[Dict[str, str]]:
def __str__(self) -> str:
return f"[{self.getErrorClass()}] {self.message}"
+
+
+class CapturedException(PySparkException):
+ def __init__(
+ self,
+ desc: Optional[str] = None,
+ stackTrace: Optional[str] = None,
+ cause: Optional[Py4JJavaError] = None,
+ origin: Optional[Py4JJavaError] = None,
+ ):
+ # desc & stackTrace vs origin are mutually exclusive.
+ # cause is optional.
+ assert (origin is not None and desc is None and stackTrace is None) or
(
+ origin is None and desc is not None and stackTrace is not None
+ )
+
+ self.desc = desc if desc is not None else cast(Py4JJavaError,
origin).getMessage()
+ assert SparkContext._jvm is not None
+ self.stackTrace = (
+ stackTrace
+ if stackTrace is not None
+ else
(SparkContext._jvm.org.apache.spark.util.Utils.exceptionString(origin))
+ )
+ self.cause = convert_exception(cause) if cause is not None else None
+ if self.cause is None and origin is not None and origin.getCause() is
not None:
+ self.cause = convert_exception(origin.getCause())
+ self._origin = origin
+
+ def __str__(self) -> str:
+ assert SparkContext._jvm is not None
+
+ jvm = SparkContext._jvm
+ sql_conf = jvm.org.apache.spark.sql.internal.SQLConf.get()
+ debug_enabled = sql_conf.pysparkJVMStacktraceEnabled()
+ desc = self.desc
+ if debug_enabled:
+ desc = desc + "\n\nJVM stacktrace:\n%s" % self.stackTrace
+ return str(desc)
+
+ def getErrorClass(self) -> Optional[str]:
+ assert SparkContext._gateway is not None
+
+ gw = SparkContext._gateway
+ if self._origin is not None and is_instance_of(
+ gw, self._origin, "org.apache.spark.SparkThrowable"
+ ):
+ return self._origin.getErrorClass()
+ else:
+ return None
+
+ def getSqlState(self) -> Optional[str]:
+ assert SparkContext._gateway is not None
+
+ gw = SparkContext._gateway
+ if self._origin is not None and is_instance_of(
+ gw, self._origin, "org.apache.spark.SparkThrowable"
+ ):
+ return self._origin.getSqlState()
+ else:
+ return None
+
+
+def convert_exception(e: Py4JJavaError) -> CapturedException:
Review Comment:
If you're exposing these classes as API, should list up the classes with
`__all__`, and make it documented.
--
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]