hvanhovell commented on code in PR #42377:
URL: https://github.com/apache/spark/pull/42377#discussion_r1330596618


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/utils/ErrorUtils.scala:
##########
@@ -57,7 +76,59 @@ private[connect] object ErrorUtils extends Logging {
     classes.toSeq
   }
 
-  private def buildStatusFromThrowable(st: Throwable, stackTraceEnabled: 
Boolean): RPCStatus = {
+  // The maximum length of the error chain.
+  private val MAX_ERROR_CHAIN_LENGTH = 5
+
+  /**
+   * Convert Throwable to a protobuf message FetchErrorDetailsResponse.
+   * @param st
+   *   the Throwable to be converted
+   * @param serverStackTraceEnabled
+   *   whether to return the server stack trace.
+   * @return
+   *   FetchErrorDetailsResponse
+   */
+  private[connect] def throwableToFetchErrorDetailsResponse(
+      st: Throwable,
+      serverStackTraceEnabled: Boolean = false): FetchErrorDetailsResponse = {
+    val errors =
+      traverseCauses(st).take(MAX_ERROR_CHAIN_LENGTH).zipWithIndex.map { case 
(error, idx) =>
+        val builder = FetchErrorDetailsResponse.Error
+          .newBuilder()
+          .setMessage(error.getMessage)
+          
.addAllErrorTypeHierarchy(ErrorUtils.allClasses(error.getClass).map(_.getName).asJava)
+
+        if (error.getCause != null) {
+          builder.setCauseIdx(idx + 1)

Review Comment:
   Sorry for harping on here, two things:
   1. This breaks if reach `MAX_ERROR_CHAIN_LENGTH`, you will be point to an 
exception you cannot reach.
   2. In terms of control this is less than great because you assume that 
`traverseCauses` produces a certain order.  In this case an imperative loop 
might have been better.



-- 
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]

Reply via email to