|
16 | 16 | # |
17 | 17 |
|
18 | 18 | import py4j |
| 19 | +from py4j.java_gateway import is_instance_of |
19 | 20 |
|
20 | 21 | from pyspark import SparkContext |
21 | 22 |
|
22 | 23 |
|
23 | 24 | class CapturedException(Exception): |
24 | | - def __init__(self, desc, stackTrace, cause=None): |
25 | | - self.desc = desc |
26 | | - self.stackTrace = stackTrace |
| 25 | + def __init__(self, desc=None, stackTrace=None, cause=None, origin=None): |
| 26 | + # desc & stackTrace vs origin are mutually exclusive. |
| 27 | + # cause is optional. |
| 28 | + assert ((origin is not None and desc is None and stackTrace is None) |
| 29 | + or (origin is None and desc is not None and stackTrace is not None)) |
| 30 | + |
| 31 | + self.desc = desc if desc is not None else origin.getMessage() |
| 32 | + self.stackTrace = ( |
| 33 | + stackTrace if stackTrace is not None |
| 34 | + else SparkContext._jvm.org.apache.spark.util.Utils.exceptionString(origin) |
| 35 | + ) |
27 | 36 | self.cause = convert_exception(cause) if cause is not None else None |
| 37 | + if self.cause is None and origin is not None and origin.getCause() is not None: |
| 38 | + self.cause = convert_exception(origin.getCause()) |
| 39 | + self._origin = origin |
28 | 40 |
|
29 | 41 | def __str__(self): |
30 | | - sql_conf = SparkContext._jvm.org.apache.spark.sql.internal.SQLConf.get() |
| 42 | + assert SparkContext._jvm is not None |
| 43 | + |
| 44 | + jvm = SparkContext._jvm |
| 45 | + sql_conf = jvm.org.apache.spark.sql.internal.SQLConf.get() |
31 | 46 | debug_enabled = sql_conf.pysparkJVMStacktraceEnabled() |
32 | 47 | desc = self.desc |
33 | 48 | if debug_enabled: |
34 | 49 | desc = desc + "\n\nJVM stacktrace:\n%s" % self.stackTrace |
35 | 50 | return str(desc) |
36 | 51 |
|
| 52 | + def getErrorClass(self): |
| 53 | + assert SparkContext._gateway is not None |
| 54 | + |
| 55 | + gw = SparkContext._gateway |
| 56 | + if self._origin is not None and is_instance_of( |
| 57 | + gw, self._origin, "org.apache.spark.SparkThrowable"): |
| 58 | + return self._origin.getErrorClass() |
| 59 | + else: |
| 60 | + return None |
| 61 | + |
| 62 | + def getSqlState(self): |
| 63 | + assert SparkContext._gateway is not None |
| 64 | + |
| 65 | + gw = SparkContext._gateway |
| 66 | + if self._origin is not None and is_instance_of( |
| 67 | + gw, self._origin, "org.apache.spark.SparkThrowable"): |
| 68 | + return self._origin.getSqlState() |
| 69 | + else: |
| 70 | + return None |
| 71 | + |
37 | 72 |
|
38 | 73 | class AnalysisException(CapturedException): |
39 | 74 | """ |
@@ -78,31 +113,37 @@ class UnknownException(CapturedException): |
78 | 113 |
|
79 | 114 |
|
80 | 115 | def convert_exception(e): |
81 | | - s = e.toString() |
| 116 | + assert e is not None |
| 117 | + assert SparkContext._jvm is not None |
| 118 | + assert SparkContext._gateway is not None |
| 119 | + |
| 120 | + jvm = SparkContext._jvm |
| 121 | + gw = SparkContext._gateway |
| 122 | + |
| 123 | + if is_instance_of(gw, e, "org.apache.spark.sql.catalyst.parser.ParseException"): |
| 124 | + return ParseException(origin=e) |
| 125 | + # Order matters. ParseException inherits AnalysisException. |
| 126 | + elif is_instance_of(gw, e, 'org.apache.spark.sql.AnalysisException'): |
| 127 | + return AnalysisException(origin=e) |
| 128 | + elif is_instance_of(gw, e, 'org.apache.spark.sql.streaming.StreamingQueryException'): |
| 129 | + return StreamingQueryException(origin=e) |
| 130 | + elif is_instance_of(gw, e, 'org.apache.spark.sql.execution.QueryExecutionException'): |
| 131 | + return QueryExecutionException(origin=e) |
| 132 | + elif is_instance_of(gw, e, 'java.lang.IllegalArgumentException'): |
| 133 | + return IllegalArgumentException(origin=e) |
| 134 | + |
82 | 135 | c = e.getCause() |
83 | | - stacktrace = SparkContext._jvm.org.apache.spark.util.Utils.exceptionString(e) |
84 | | - |
85 | | - if s.startswith('org.apache.spark.sql.AnalysisException: '): |
86 | | - return AnalysisException(s.split(': ', 1)[1], stacktrace, c) |
87 | | - if s.startswith('org.apache.spark.sql.catalyst.analysis'): |
88 | | - return AnalysisException(s.split(': ', 1)[1], stacktrace, c) |
89 | | - if s.startswith('org.apache.spark.sql.catalyst.parser.ParseException: '): |
90 | | - return ParseException(s.split(': ', 1)[1], stacktrace, c) |
91 | | - if s.startswith('org.apache.spark.sql.streaming.StreamingQueryException: '): |
92 | | - return StreamingQueryException(s.split(': ', 1)[1], stacktrace, c) |
93 | | - if s.startswith('org.apache.spark.sql.execution.QueryExecutionException: '): |
94 | | - return QueryExecutionException(s.split(': ', 1)[1], stacktrace, c) |
95 | | - if s.startswith('java.lang.IllegalArgumentException: '): |
96 | | - return IllegalArgumentException(s.split(': ', 1)[1], stacktrace, c) |
| 136 | + stacktrace = jvm.org.apache.spark.util.Utils.exceptionString(e) |
97 | 137 | if c is not None and ( |
98 | | - c.toString().startswith('org.apache.spark.api.python.PythonException: ') |
| 138 | + is_instance_of(gw, c, 'org.apache.spark.api.python.PythonException') |
99 | 139 | # To make sure this only catches Python UDFs. |
100 | 140 | and any(map(lambda v: "org.apache.spark.sql.execution.python" in v.toString(), |
101 | 141 | c.getStackTrace()))): |
102 | 142 | msg = ("\n An exception was thrown from the Python worker. " |
103 | 143 | "Please see the stack trace below.\n%s" % c.getMessage()) |
104 | 144 | return PythonException(msg, stacktrace) |
105 | | - return UnknownException(s, stacktrace, c) |
| 145 | + |
| 146 | + return UnknownException(desc=e.toString(), stackTrace=stacktrace, cause=c) |
106 | 147 |
|
107 | 148 |
|
108 | 149 | def capture_sql_exception(f): |
|
0 commit comments