[python] Fix database error handler about no datasource name (#7631)

* [python] Fix database error handler about no datasource name

The original code show too many unrelated log when database name
do not exists. BTW, it would continue the code when error miss.
This patch is try to fix it.

close: #7616

* Use java exception instead of self define

* Enhance
This commit is contained in:
Jiajie Zhong 2022-01-07 21:29:37 +08:00 committed by GitHub
parent 0b38d0e826
commit 65322155a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -19,6 +19,9 @@
from typing import Dict
from py4j.protocol import Py4JJavaError
from pydolphinscheduler.exceptions import PyDSParamException
from pydolphinscheduler.java_gateway import launch_gateway
@ -52,5 +55,9 @@ class Database(dict):
return self._database
else:
gateway = launch_gateway()
self._database = gateway.entry_point.getDatasourceInfo(name)
try:
self._database = gateway.entry_point.getDatasourceInfo(name)
# Handler database source do not exists error, for now we just terminate the process.
except Py4JJavaError as ex:
raise PyDSParamException(str(ex.java_exception))
return self._database

View File

@ -392,12 +392,12 @@ public class PythonGatewayServer extends SpringBootServletInitializer {
public Map<String, Object> getDatasourceInfo(String datasourceName) {
Map<String, Object> result = new HashMap<>();
List<DataSource> dataSourceList = dataSourceMapper.queryDataSourceByName(datasourceName);
if (dataSourceList.size() > 1) {
String msg = String.format("Get more than one datasource by name %s", datasourceName);
if (dataSourceList == null || dataSourceList.isEmpty()) {
String msg = String.format("Can not find any datasource by name %s", datasourceName);
logger.error(msg);
throw new IllegalArgumentException(msg);
} else if (dataSourceList.size() == 0) {
String msg = String.format("Can not find any datasource by name %s", datasourceName);
} else if (dataSourceList.size() > 1) {
String msg = String.format("Get more than one datasource by name %s", datasourceName);
logger.error(msg);
throw new IllegalArgumentException(msg);
} else {