Skip to content

Commit 3dfa4ea

Browse files
viiryadavies
authored andcommitted
[SPARK-11322] [PYSPARK] Keep full stack trace in captured exception
JIRA: https://issues.apache.org/jira/browse/SPARK-11322 As reported by JoshRosen in [databricks/spark-redshift/issues/89](databricks/spark-redshift#89 (comment)), the exception-masking behavior sometimes makes debugging harder. To deal with this issue, we should keep full stack trace in the captured exception. Author: Liang-Chi Hsieh <[email protected]> Closes #9283 from viirya/py-exception-stacktrace.
1 parent 0cb7662 commit 3dfa4ea

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

python/pyspark/sql/tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,12 @@ def test_capture_illegalargument_exception(self):
10791079
df = self.sqlCtx.createDataFrame([(1, 2)], ["a", "b"])
10801080
self.assertRaisesRegexp(IllegalArgumentException, "1024 is not in the permitted values",
10811081
lambda: df.select(sha2(df.a, 1024)).collect())
1082+
try:
1083+
df.select(sha2(df.a, 1024)).collect()
1084+
except IllegalArgumentException as e:
1085+
self.assertRegexpMatches(e.desc, "1024 is not in the permitted values")
1086+
self.assertRegexpMatches(e.stackTrace,
1087+
"org.apache.spark.sql.functions")
10821088

10831089
def test_with_column_with_existing_name(self):
10841090
keys = self.df.withColumn("key", self.df.key).select("key").collect()

python/pyspark/sql/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@
1818
import py4j
1919

2020

21-
class AnalysisException(Exception):
21+
class CapturedException(Exception):
22+
def __init__(self, desc, stackTrace):
23+
self.desc = desc
24+
self.stackTrace = stackTrace
25+
26+
def __str__(self):
27+
return repr(self.desc)
28+
29+
30+
class AnalysisException(CapturedException):
2231
"""
2332
Failed to analyze a SQL query plan.
2433
"""
2534

2635

27-
class IllegalArgumentException(Exception):
36+
class IllegalArgumentException(CapturedException):
2837
"""
2938
Passed an illegal or inappropriate argument.
3039
"""
@@ -36,10 +45,12 @@ def deco(*a, **kw):
3645
return f(*a, **kw)
3746
except py4j.protocol.Py4JJavaError as e:
3847
s = e.java_exception.toString()
48+
stackTrace = '\n\t at '.join(map(lambda x: x.toString(),
49+
e.java_exception.getStackTrace()))
3950
if s.startswith('org.apache.spark.sql.AnalysisException: '):
40-
raise AnalysisException(s.split(': ', 1)[1])
51+
raise AnalysisException(s.split(': ', 1)[1], stackTrace)
4152
if s.startswith('java.lang.IllegalArgumentException: '):
42-
raise IllegalArgumentException(s.split(': ', 1)[1])
53+
raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
4354
raise
4455
return deco
4556

0 commit comments

Comments
 (0)