Skip to content

Commit b4c6053

Browse files
committed
Wrap and re-throw Await.result exceptions in order to capture full stacktrace
Exceptions thrown from Scala's `Await.result` don't include the waiting thread's stacktrace, making it hard to figure out where errors occur. Similar to the fix implemented in Spark in apache/spark#12433, this patch modifies our `Await.result` usages to wrap and rethrow exceptions to capture the calling thread's stack. Author: Josh Rosen <joshrosen@databricks.com> Closes #299 from JoshRosen/better-error-reporting.
1 parent 1092c7c commit b4c6053

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/it/scala/com/databricks/spark/redshift/IAMIntegrationSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class IAMIntegrationSuite extends IntegrationSuiteBase {
6868
.mode(SaveMode.ErrorIfExists)
6969
.save()
7070
}
71-
assert(err.getMessage.contains("is not authorized to assume IAM Role"))
71+
assert(err.getCause.getMessage.contains("is not authorized to assume IAM Role"))
7272
} finally {
7373
conn.prepareStatement(s"drop table if exists $tableName").executeUpdate()
7474
conn.commit()

src/main/scala/com/databricks/spark/redshift/RedshiftJDBCWrapper.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import scala.collection.JavaConverters._
2626
import scala.concurrent.{Await, ExecutionContext, Future}
2727
import scala.concurrent.duration.Duration
2828
import scala.util.Try
29+
import scala.util.control.NonFatal
2930

3031
import org.apache.spark.SPARK_VERSION
3132
import org.apache.spark.sql.execution.datasources.jdbc.DriverRegistry
@@ -124,7 +125,16 @@ private[redshift] class JDBCWrapper {
124125
op: PreparedStatement => T): T = {
125126
try {
126127
val future = Future[T](op(statement))(ec)
127-
Await.result(future, Duration.Inf)
128+
try {
129+
Await.result(future, Duration.Inf)
130+
} catch {
131+
case e: SQLException =>
132+
// Wrap and re-throw so that this thread's stacktrace appears to the user.
133+
throw new SQLException("Exception thrown in awaitResult: ", e)
134+
case NonFatal(t) =>
135+
// Wrap and re-throw so that this thread's stacktrace appears to the user.
136+
throw new Exception("Exception thrown in awaitResult: ", t)
137+
}
128138
} catch {
129139
case e: InterruptedException =>
130140
try {

0 commit comments

Comments
 (0)