Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.pf4j.Extension;
import org.pf4j.PluginWrapper;
import org.postgresql.util.PGobject;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -687,6 +689,9 @@ public Set<String> validateDatasource(DatasourceConfiguration datasourceConfigur
invalids.add(
String.format(PostgresErrorMessages.DS_INVALID_HOSTNAME_ERROR_MSG, endpoint.getHost()));
}
if (StringUtils.isEmpty(endpoint.getPort())) {
invalids.add(PostgresErrorMessages.DS_MISSING_PORT_ERROR_MSG);
}
}
}

Expand Down Expand Up @@ -1320,10 +1325,27 @@ private static HikariDataSource createConnectionPool(
try {
datasource = new HikariDataSource(config);
} catch (PoolInitializationException e) {
throw new AppsmithPluginException(
AppsmithPluginError.PLUGIN_DATASOURCE_ARGUMENT_ERROR,
PostgresErrorMessages.CONNECTION_POOL_CREATION_FAILED_ERROR_MSG,
e.getMessage());
Throwable cause = e.getCause();
if (cause instanceof PSQLException) {
PSQLException psqlException = (PSQLException) cause;
String sqlState = psqlException.getSQLState();
if (PSQLState.CONNECTION_UNABLE_TO_CONNECT.getState().equals(sqlState)) {
throw new AppsmithPluginException(
AppsmithPluginError.PLUGIN_DATASOURCE_ARGUMENT_ERROR,
PostgresErrorMessages.DS_INVALID_HOSTNAME_AND_PORT_MSG,
psqlException.getMessage());
} else {
throw new AppsmithPluginException(
AppsmithPluginError.PLUGIN_DATASOURCE_ARGUMENT_ERROR,
PostgresErrorMessages.CONNECTION_POOL_CREATION_FAILED_ERROR_MSG,
cause.getMessage());
}
} else {
throw new AppsmithPluginException(
AppsmithPluginError.PLUGIN_DATASOURCE_ARGUMENT_ERROR,
PostgresErrorMessages.CONNECTION_POOL_CREATION_FAILED_ERROR_MSG,
cause != null ? cause.getMessage() : e.getMessage());
}
}

return datasource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class PostgresErrorMessages extends BasePluginErrorMessages {

public static final String DS_MISSING_HOSTNAME_ERROR_MSG = "Missing hostname.";

public static final String DS_MISSING_PORT_ERROR_MSG = "Missing port.";

public static final String DS_INVALID_HOSTNAME_ERROR_MSG =
"Host value cannot contain `/` or `:` characters. Found `%s`.";

Expand All @@ -49,4 +51,6 @@ public class PostgresErrorMessages extends BasePluginErrorMessages {
public static final String DS_MISSING_PASSWORD_ERROR_MSG = "Missing password for authentication.";

public static final String DS_MISSING_DATABASE_NAME_ERROR_MSG = "Missing database name.";

public static final String DS_INVALID_HOSTNAME_AND_PORT_MSG = "Please check the host and port.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,15 @@ public void itShouldValidateDatasourceWithEmptyHost() {
assertEquals(Set.of("Missing hostname."), pluginExecutor.validateDatasource(dsConfig));
}

@Test
public void itShouldValidateDatasourceWithEmptyPort() {

DatasourceConfiguration dsConfig = createDatasourceConfiguration();
dsConfig.getEndpoints().get(0).setPort(null);

assertEquals(Set.of("Missing port."), pluginExecutor.validateDatasource(dsConfig));
}

@Test
public void itShouldValidateDatasourceWithInvalidHostname() {

Expand Down