This is an automated email from the ASF dual-hosted git repository. dianfu pushed a commit to branch release-1.9 in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/release-1.9 by this push: new e571b64 [FLINK-17945][python] Improve the error message when instantiating non-existing Java class e571b64 is described below commit e571b64032ceabab83c852ed7ed8a5292e2c5e85 Author: Dian Fu <dia...@apache.org> AuthorDate: Wed Jun 3 22:42:19 2020 +0800 [FLINK-17945][python] Improve the error message when instantiating non-existing Java class This closes #12469. --- flink-python/pyflink/java_gateway.py | 3 ++- flink-python/pyflink/util/exceptions.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/flink-python/pyflink/java_gateway.py b/flink-python/pyflink/java_gateway.py index 9bb0b62..a583669 100644 --- a/flink-python/pyflink/java_gateway.py +++ b/flink-python/pyflink/java_gateway.py @@ -28,7 +28,7 @@ from threading import RLock from py4j.java_gateway import java_import, JavaGateway, GatewayParameters from pyflink.find_flink_home import _find_flink_home -from pyflink.util.exceptions import install_exception_handler +from pyflink.util.exceptions import install_exception_handler, install_py4j_hooks _gateway = None _lock = RLock() @@ -51,6 +51,7 @@ def get_gateway(): # import the flink view import_flink_view(_gateway) install_exception_handler() + install_py4j_hooks() return _gateway diff --git a/flink-python/pyflink/util/exceptions.py b/flink-python/pyflink/util/exceptions.py index 2a28936..5c742ae 100644 --- a/flink-python/pyflink/util/exceptions.py +++ b/flink-python/pyflink/util/exceptions.py @@ -172,3 +172,15 @@ def install_exception_handler(): patched = capture_java_exception(original) # only patch the one used in py4j.java_gateway (call Java API) py4j.java_gateway.get_return_value = patched + + +def install_py4j_hooks(): + """ + Hook the classes such as JavaPackage, etc of Py4j to improve the exception message. + """ + def wrapped_call(self, *args, **kwargs): + raise TypeError( + "Could not found the Java class '%s'. The Java dependencies could be specified via " + "command line argument '--jarfile'" % self._fqn) + + setattr(py4j.java_gateway.JavaPackage, '__call__', wrapped_call)