You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/12/20 00:24:19 UTC

[GitHub] [spark] HyukjinKwon commented on a diff in pull request #39128: [SPARK-41586][PYTHON] Introduce new PySpark package: `pyspark.errors` and error classes.

HyukjinKwon commented on code in PR #39128:
URL: https://github.com/apache/spark/pull/39128#discussion_r1052750600


##########
python/pyspark/errors/__init__.py:
##########
@@ -0,0 +1,140 @@
+#
+# 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.
+#
+
+from typing import Dict, Optional, Union, Any, Type
+from pyspark.errors.error_classes import ERROR_CLASSES
+
+
+class PySparkException(Exception):

Review Comment:
   Should we integrate this to the exceptions defined under `pyspark.sql.utils`?



##########
python/pyspark/errors/__init__.py:
##########
@@ -0,0 +1,140 @@
+#
+# 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.
+#
+
+from typing import Dict, Optional, Union, Any, Type
+from pyspark.errors.error_classes import ERROR_CLASSES
+
+
+class PySparkException(Exception):
+    """
+    Base Exception for handling the errors generated by PySpark
+    """
+
+    def __init__(self, error_class: str, message_parameters: Optional[Dict[str, str]] = None):
+        self._verify_error_class(error_class)
+        self._error_class = error_class
+
+        self._error_message_format = ERROR_CLASSES[error_class]
+
+        self._verify_message_parameters(message_parameters)
+        self._message_parameters = message_parameters
+
+    def _verify_error_class(self, error_class: str) -> None:
+        assert (
+            error_class in ERROR_CLASSES
+        ), f"{error_class} is not in the list of error classes: {list(ERROR_CLASSES.keys())}"
+
+    def _verify_message_parameters(
+        self, message_parameters: Optional[Dict[str, str]] = None
+    ) -> None:
+        required = set(self._error_message_format.__code__.co_varnames)
+        given = set() if message_parameters is None else set(message_parameters.keys())
+        assert given == required, f"Given message parameters: {given} , but {required} required"
+
+    def getErrorClass(self) -> str:
+        return self._error_class
+
+    def getMessageParameters(self) -> Optional[Dict[str, str]]:
+        return self._message_parameters
+
+    def getErrorMessage(self) -> str:
+        if self._message_parameters is None:
+            message = self._error_message_format()  # type: ignore[operator]
+        else:
+            message = self._error_message_format(
+                *self._message_parameters.values()
+            )  # type: ignore[operator]
+
+        return message
+
+    def __str__(self) -> str:
+        # The user-facing error message is contains error class and error message
+        # e.g. "[WRONG_NUM_COLUMNS] 'greatest' should take at least two columns"
+        return f"[{self.getErrorClass()}] {self.getErrorMessage()}"
+
+
+def notColumnOrStringError(arg_name: str, arg_type: Type[Any]) -> "PySparkException":
+    return PySparkException(
+        error_class="NOT_COLUMN_OR_STRING",
+        message_parameters={"arg_name": arg_name, "arg_type": arg_type.__name__},
+    )
+
+
+def notColumnOrIntegerError(arg_name: str, arg_type: Type[Any]) -> "PySparkException":

Review Comment:
   Can we follow snake naming rule since these are all internals?



-- 
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-unsubscribe@spark.apache.org

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


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