cloud-fan commented on code in PR #37969:
URL: https://github.com/apache/spark/pull/37969#discussion_r978415586


##########
core/src/main/scala/org/apache/spark/ErrorClassesJSONReader.scala:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark
+
+import java.net.URL
+
+import scala.collection.JavaConverters._
+import scala.collection.immutable.SortedMap
+
+import com.fasterxml.jackson.annotation.JsonIgnore
+import com.fasterxml.jackson.core.`type`.TypeReference
+import com.fasterxml.jackson.databind.json.JsonMapper
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import org.apache.commons.text.StringSubstitutor
+
+import org.apache.spark.annotation.DeveloperApi
+
+/**
+ * A reader to load error information from one or more JSON files. Note that, 
if one error appears
+ * in more than one JSON files, the latter wins.
+ */
+@DeveloperApi
+class ErrorClassesJsonReader(jsonFileURLs: Seq[URL]) {
+  assert(jsonFileURLs.nonEmpty)
+
+  private def readAsMap(url: URL): SortedMap[String, ErrorInfo] = {
+    val mapper: JsonMapper = JsonMapper.builder()
+      .addModule(DefaultScalaModule)
+      .build()
+    mapper.readValue(url, new TypeReference[SortedMap[String, ErrorInfo]]() {})
+  }
+
+  // Exposed for testing
+  private[spark] val errorInfoMap = jsonFileURLs.map(readAsMap).reduce(_ ++ _)
+
+  def getErrorMessage(errorClass: String, messageParameters: Map[String, 
String]): String = {
+    val errorClasses = errorClass.split("\\.")
+    assert(errorClasses.length == 1 || errorClasses.length == 2)
+
+    val mainErrorClass = errorClasses.head
+    val subErrorClass = errorClasses.tail.headOption
+    val errorInfo = errorInfoMap.getOrElse(
+      mainErrorClass,
+      throw SparkException.internalError(s"Cannot find error class 
'$errorClass'"))
+    assert(errorInfo.subClass.isDefined == subErrorClass.isDefined)
+
+    val finalFormat = if (subErrorClass.isEmpty) {
+      errorInfo.messageFormat
+    } else {
+      val errorSubInfo = errorInfo.subClass.get.getOrElse(
+        subErrorClass.get,
+        throw SparkException.internalError(s"Cannot find sub error class 
'${subErrorClass.get}'"))

Review Comment:
   Because this means a bug and it's better to throw intenal error.



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to