Skip to content

Commit bc44d82

Browse files
committed
Show password dialog only for access denied exception, for other exceptions, show warn notification instead.
1 parent 9e74abd commit bc44d82

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/link/mysql/MySQLConnectionUtils.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static boolean connect(String url, String username, String password) {
3333
}
3434

3535
public static ConnectResult connectWithPing(String url, String username, String password) {
36+
int errorCode = 0;
3637
boolean connected = false;
3738
String errorMessage = null;
3839
Long pingCost = null;
@@ -51,17 +52,20 @@ public static ConnectResult connectWithPing(String url, String username, String
5152
}
5253
pingCost = System.currentTimeMillis() - start;
5354
serverVersion = ((ConnectionImpl) connection).getServerVersion().toString();
54-
} catch (ClassNotFoundException | SQLException exception) {
55+
} catch (ClassNotFoundException exception) {
56+
errorMessage = exception.getMessage();
57+
} catch (SQLException exception) {
58+
errorCode = exception.getErrorCode();
5559
errorMessage = isConnectionIssue(exception) ? String.format(CONNECTION_ISSUE_MESSAGE, exception.getMessage()) : exception.getMessage();
5660
}
5761
EventUtil.logEvent(EventType.info, ActionConstants.parse(ActionConstants.MySQL.TEST_CONNECTION).getServiceName(),
5862
ActionConstants.parse(ActionConstants.MySQL.TEST_CONNECTION).getOperationName(),
5963
Collections.singletonMap("result", String.valueOf(connected)));
60-
return new ConnectResult(connected, errorMessage, pingCost, serverVersion);
64+
return new ConnectResult(connected, errorMessage, pingCost, serverVersion, errorCode);
6165
}
6266

63-
private static boolean isConnectionIssue(final Exception exception){
64-
return exception instanceof SQLException && ((SQLException) exception).getErrorCode() == CONNECTION_ERROR_CODE;
67+
private static boolean isConnectionIssue(final SQLException exception){
68+
return exception.getErrorCode() == CONNECTION_ERROR_CODE;
6569
}
6670

6771
@Getter
@@ -71,5 +75,6 @@ public static class ConnectResult {
7175
private String message;
7276
private Long pingCost;
7377
private String serverVersion;
78+
private int errorCode;
7479
}
7580
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/lib/link/AzureLinkService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
2222
import com.microsoft.intellij.AzureLinkStorage;
2323
import com.microsoft.intellij.AzureMySQLStorage;
24+
import com.microsoft.intellij.util.PluginUtil;
2425
import org.apache.commons.codec.digest.DigestUtils;
2526
import org.apache.commons.collections4.CollectionUtils;
2627
import org.apache.commons.lang3.ArrayUtils;
@@ -32,6 +33,7 @@
3233

3334
public class AzureLinkService {
3435
private static final AzureLinkService instance = new AzureLinkService();
36+
private static final int ACCESS_DENIED_ERROR_CODE = 1045;
3537

3638
public static AzureLinkService getInstance() {
3739
return instance;
@@ -109,10 +111,15 @@ public Map<String, String> retrieveLinkEnvsByModuleName(Project project, String
109111
}
110112

111113
private String readPasswordCredentials(Project project, MySQLResourcePO service) {
112-
String storagedPassword = AzureMySQLStorage.getStorage().loadPassword(service, service.getPasswordSave(), service.getUsername());
113-
if (StringUtils.isNotBlank(storagedPassword)) {
114-
if (MySQLConnectionUtils.connect(service.getUrl(), service.getUsername(), storagedPassword)) {
115-
return storagedPassword;
114+
final String storedPassword = AzureMySQLStorage.getStorage().loadPassword(service, service.getPasswordSave(), service.getUsername());
115+
if (StringUtils.isNotEmpty(storedPassword)) {
116+
final MySQLConnectionUtils.ConnectResult result = MySQLConnectionUtils.connectWithPing(service.getUrl(), service.getUsername(), storedPassword);
117+
if (result.isConnected()) {
118+
return storedPassword;
119+
}
120+
if (result.getErrorCode() != ACCESS_DENIED_ERROR_CODE) {
121+
PluginUtil.showWarnNotification("Failed to connect MySQL", result.getMessage());
122+
return StringUtils.EMPTY;
116123
}
117124
}
118125
// re-input password

0 commit comments

Comments
 (0)