From d3088e76d44c09901588040e37a6cc473d9073db Mon Sep 17 00:00:00 2001 From: qianjinshen Date: Wed, 30 Sep 2020 11:35:28 +0800 Subject: [PATCH] merged error enums in a single class. --- .../serviceexplorer/azure/AzureModule.java | 16 ++--- .../azuretools/entity/BaseEntity.java | 52 -------------- .../azuretools/entity/ResponseWrapper.java | 70 ------------------- .../microsoft/azuretools/enums/ErrorEnum.java | 26 +++++-- .../azuretools/enums/ErrorUIMapEnum.java | 66 ----------------- .../azuretools/enums/ErrorUIMessageEnum.java | 44 ------------ .../AzureRuntimeException.java | 2 +- .../sdkmanage/AzureCliAzureManager.java | 2 +- .../sdkmanage/AzureManagerBase.java | 52 +++++--------- 9 files changed, 50 insertions(+), 280 deletions(-) delete mode 100644 Utils/azuretools-core/src/com/microsoft/azuretools/entity/BaseEntity.java delete mode 100644 Utils/azuretools-core/src/com/microsoft/azuretools/entity/ResponseWrapper.java delete mode 100644 Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMapEnum.java delete mode 100644 Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMessageEnum.java rename Utils/azuretools-core/src/com/microsoft/azuretools/{authmanage/srvpri/exceptions => exception}/AzureRuntimeException.java (96%) diff --git a/Utils/azure-explorer-common/src/com/microsoft/tooling/msservices/serviceexplorer/azure/AzureModule.java b/Utils/azure-explorer-common/src/com/microsoft/tooling/msservices/serviceexplorer/azure/AzureModule.java index 1fa1185d96..df9d93ca2b 100644 --- a/Utils/azure-explorer-common/src/com/microsoft/tooling/msservices/serviceexplorer/azure/AzureModule.java +++ b/Utils/azure-explorer-common/src/com/microsoft/tooling/msservices/serviceexplorer/azure/AzureModule.java @@ -26,11 +26,11 @@ import com.microsoft.azuretools.authmanage.AuthMethodManager; import com.microsoft.azuretools.authmanage.SubscriptionManager; import com.microsoft.azuretools.authmanage.models.SubscriptionDetail; -import com.microsoft.azuretools.authmanage.srvpri.exceptions.AzureRuntimeException; +import com.microsoft.azuretools.enums.ErrorEnum; +import com.microsoft.azuretools.exception.AzureRuntimeException; import com.microsoft.azuretools.azurecommons.helpers.AzureCmdException; import com.microsoft.azuretools.azurecommons.helpers.NotNull; import com.microsoft.azuretools.azurecommons.helpers.Nullable; -import com.microsoft.azuretools.enums.ErrorUIMapEnum; import com.microsoft.azuretools.sdkmanage.AzureManager; import com.microsoft.tooling.msservices.components.DefaultLoader; import com.microsoft.tooling.msservices.serviceexplorer.AzureRefreshableNode; @@ -123,14 +123,12 @@ private static String composeName() { .filter(SubscriptionDetail::isSelected).collect(Collectors.toList()); return String.format("%s (%s)", BASE_MODULE_NAME, getAccountDescription(selectedSubscriptions)); + } catch (AzureRuntimeException e) { + DefaultLoader.getUIHelper().showInfoNotification( + ERROR_GETTING_SUBSCRIPTIONS_TITLE, ErrorEnum.getDisplayMessageByCode(e.getCode())); } catch (Exception e) { - if (e instanceof AzureRuntimeException) { - DefaultLoader.getUIHelper().showInfoNotification( - ERROR_GETTING_SUBSCRIPTIONS_TITLE, ErrorUIMapEnum.getViewMessageByCode(((AzureRuntimeException) e).getCode())); - } else { - final String msg = String.format(ERROR_GETTING_SUBSCRIPTIONS_MESSAGE, e.getMessage()); - DefaultLoader.getUIHelper().showException(msg, e, ERROR_GETTING_SUBSCRIPTIONS_TITLE, false, true); - } + final String msg = String.format(ERROR_GETTING_SUBSCRIPTIONS_MESSAGE, e.getMessage()); + DefaultLoader.getUIHelper().showException(msg, e, ERROR_GETTING_SUBSCRIPTIONS_TITLE, false, true); } return BASE_MODULE_NAME; } diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/entity/BaseEntity.java b/Utils/azuretools-core/src/com/microsoft/azuretools/entity/BaseEntity.java deleted file mode 100644 index c8abd65685..0000000000 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/entity/BaseEntity.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation - * - * All rights reserved. - * - * MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.microsoft.azuretools.entity; - -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; - -import java.io.Serializable; - -/** - * Base entity to implement hashCode & equals & toString with reflection for its sub classes. - */ -public abstract class BaseEntity implements Serializable { - - @Override - public int hashCode() { - return HashCodeBuilder.reflectionHashCode(this); - } - - @Override - public boolean equals(Object obj) { - return EqualsBuilder.reflectionEquals(this, obj); - } - - @Override - public String toString() { - return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - -} diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/entity/ResponseWrapper.java b/Utils/azuretools-core/src/com/microsoft/azuretools/entity/ResponseWrapper.java deleted file mode 100644 index e5eeb88630..0000000000 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/entity/ResponseWrapper.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation - * - * All rights reserved. - * - * MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.microsoft.azuretools.entity; - -/** - * Wrapper Entify for remote procedure call response. - * @TODOs try to seperate data and error into different classes. - */ -public class ResponseWrapper extends BaseEntity { - - private T data; - /** - * default is 0, and 0 means no error. - */ - private int errorCode; - private String errorMessage; - private String originalMessage; - - public int getErrorCode() { - return errorCode; - } - - public void setErrorCode(int errorCode) { - this.errorCode = errorCode; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public String getOriginalMessage() { - return originalMessage; - } - - public void setOriginalMessage(String originalMessage) { - this.originalMessage = originalMessage; - } - - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } -} diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorEnum.java b/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorEnum.java index a82fee4bad..844f8b0780 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorEnum.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorEnum.java @@ -26,17 +26,22 @@ * Enums of backend errors for azure tools. */ public enum ErrorEnum { - UNKNOWN_HOST_EXCEPTION(100000, "Encountered an unknown host exception."), - SOCKET_TIMEOUT_EXCEPTION(100002, "Encountered a socket timeout exception."), - FAILED_TO_GET_ACCESS_TOKEN_BY_CLI(100003, "Failed to get access token by Azure CLI command."), + UNKNOWN_HOST_EXCEPTION(100000, "Encountered an unknown host exception.", + "It seems you have an unstable network at the moment, please try again when network is available."), + SOCKET_TIMEOUT_EXCEPTION(100002, "Encountered a socket timeout exception.", + "Timeout when accessing azure, please try your operation again."), + FAILED_TO_GET_ACCESS_TOKEN_BY_CLI(100003, "Failed to get access token by Azure CLI command.", + "Failed to get access token, please try to login Azure CLI using 'az login' and try again."), ; private int errorCode; private String errorMessage; + private String displyMessage; - ErrorEnum(int errorCode, String errorMessage) { + ErrorEnum(int errorCode, String errorMessage, String displayMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; + this.displyMessage = displayMessage; } public int getErrorCode() { @@ -47,4 +52,17 @@ public String getErrorMessage() { return errorMessage; } + public String getDisplyMessage() { + return displyMessage; + } + + public static String getDisplayMessageByCode(int code) { + for (ErrorEnum e : ErrorEnum.values()) { + if (e.getErrorCode() == code) { + return e.getDisplyMessage(); + } + } + throw new IllegalArgumentException(String.format("Not found enum for code: %s", code)); + } + } diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMapEnum.java b/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMapEnum.java deleted file mode 100644 index 7157276da2..0000000000 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMapEnum.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation - * - * All rights reserved. - * - * MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.microsoft.azuretools.enums; - -import org.apache.commons.lang3.StringUtils; - -/** - * Enums of the Map of backend errors to UI messages for azure tools. - * All reminder, dialog, etc will use these contents of this class to show whatever. - */ -public enum ErrorUIMapEnum { - UNKNOWN_HOST_EXCEPTION(ErrorEnum.UNKNOWN_HOST_EXCEPTION, ErrorUIMessageEnum.UNKNOWN_HOST_EXCEPTION), - SOCKET_TIMEOUT_EXCEPTION(ErrorEnum.SOCKET_TIMEOUT_EXCEPTION, ErrorUIMessageEnum.SOCKET_TIMEOUT_EXCEPTION), - FAILED_TO_GET_ACCESS_TOKEN_BY_CLI(ErrorEnum.FAILED_TO_GET_ACCESS_TOKEN_BY_CLI, ErrorUIMessageEnum.FAILED_TO_GET_ACCESS_TOKEN_BY_CLI), - ; - - private ErrorEnum error; - private ErrorUIMessageEnum viewMessage; - - ErrorUIMapEnum(ErrorEnum error, ErrorUIMessageEnum viewMessage) { - this.error = error; - this.viewMessage = viewMessage; - } - - public ErrorEnum getError() { - return error; - } - - public ErrorUIMessageEnum getViewMessage() { - return viewMessage; - } - - public static ErrorUIMessageEnum getViewMessageEnumByCode(int code) { - for (ErrorUIMapEnum e : ErrorUIMapEnum.values()) { - if (e.getError() != null && e.getError().getErrorCode() == code) { - return e.getViewMessage(); - } - } - throw new IllegalArgumentException(String.format("Not found enum for code: %s", code)); - } - - public static String getViewMessageByCode(int code) { - ErrorUIMessageEnum viewMessage = getViewMessageEnumByCode(code); - return viewMessage != null ? viewMessage.getText() : StringUtils.EMPTY; - } -} diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMessageEnum.java b/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMessageEnum.java deleted file mode 100644 index 355e153d4d..0000000000 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/enums/ErrorUIMessageEnum.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation - * - * All rights reserved. - * - * MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and - * to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO - * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.microsoft.azuretools.enums; - -/** - * Enums of UI messages. - */ -public enum ErrorUIMessageEnum { - - UNKNOWN_HOST_EXCEPTION("It seems you have an unstable network at the moment, please try again when network is available."), - SOCKET_TIMEOUT_EXCEPTION("Timeout when accessing azure, please try your operation again."), - FAILED_TO_GET_ACCESS_TOKEN_BY_CLI("Failed to get access token, please try to login Azure CLI using 'az login' and try again."), - ; - - private String text; - - public String getText() { - return text; - } - - ErrorUIMessageEnum(String text) { - this.text = text; - } -} diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/authmanage/srvpri/exceptions/AzureRuntimeException.java b/Utils/azuretools-core/src/com/microsoft/azuretools/exception/AzureRuntimeException.java similarity index 96% rename from Utils/azuretools-core/src/com/microsoft/azuretools/authmanage/srvpri/exceptions/AzureRuntimeException.java rename to Utils/azuretools-core/src/com/microsoft/azuretools/exception/AzureRuntimeException.java index e57186d51d..21d844bba3 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/authmanage/srvpri/exceptions/AzureRuntimeException.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/exception/AzureRuntimeException.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package com.microsoft.azuretools.authmanage.srvpri.exceptions; +package com.microsoft.azuretools.exception; import com.microsoft.azuretools.enums.ErrorEnum; diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureCliAzureManager.java b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureCliAzureManager.java index 1a277ee6e6..af1ddf3914 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureCliAzureManager.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureCliAzureManager.java @@ -34,7 +34,7 @@ import com.microsoft.azuretools.authmanage.CommonSettings; import com.microsoft.azuretools.authmanage.Environment; import com.microsoft.azuretools.authmanage.models.AuthMethodDetails; -import com.microsoft.azuretools.authmanage.srvpri.exceptions.AzureRuntimeException; +import com.microsoft.azuretools.exception.AzureRuntimeException; import com.microsoft.azuretools.azurecommons.helpers.Nullable; import com.microsoft.azuretools.enums.ErrorEnum; import com.microsoft.azuretools.utils.CommandUtils; diff --git a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java index bb9bf69011..344c1465a2 100644 --- a/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java +++ b/Utils/azuretools-core/src/com/microsoft/azuretools/sdkmanage/AzureManagerBase.java @@ -32,8 +32,7 @@ import com.microsoft.azure.management.resources.Subscription; import com.microsoft.azure.management.resources.Tenant; import com.microsoft.azuretools.authmanage.*; -import com.microsoft.azuretools.authmanage.srvpri.exceptions.AzureRuntimeException; -import com.microsoft.azuretools.entity.ResponseWrapper; +import com.microsoft.azuretools.exception.AzureRuntimeException; import com.microsoft.azuretools.enums.ErrorEnum; import com.microsoft.azuretools.telemetry.TelemetryInterceptor; import com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces; @@ -43,16 +42,16 @@ import java.io.IOException; import java.net.UnknownHostException; -import java.util.*; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; -import static com.microsoft.azuretools.authmanage.Environment.CHINA; -import static com.microsoft.azuretools.authmanage.Environment.GERMAN; -import static com.microsoft.azuretools.authmanage.Environment.GLOBAL; -import static com.microsoft.azuretools.authmanage.Environment.US_GOVERNMENT; +import static com.microsoft.azuretools.authmanage.Environment.*; /** * Created by vlashch on 1/27/17. @@ -247,33 +246,20 @@ private List getSubscriptions(Azure.Authenticated tenantAuthentica } private List getTenants(Azure.Authenticated authentication) { - ResponseWrapper> responseWrapper = new ResponseWrapper<>(); - responseWrapper.setData(Collections.emptyList()); - ResponseWrapper> response = authentication.tenants().listAsync() - .toList() - .map(data -> { - responseWrapper.setData(data); - return responseWrapper; - }) - .onErrorReturn(err -> handleErrorForGetTenants(err, responseWrapper)) - .toBlocking() - .singleOrDefault(responseWrapper); - if (response.getErrorCode() != 0) { - throw new AzureRuntimeException(response.getErrorCode(), response.getErrorMessage()); - } - return response.getData(); - } - - private ResponseWrapper> handleErrorForGetTenants(Throwable err, ResponseWrapper> responseWrapper) { - LOGGER.warning(Throwables.getStackTraceAsString(err)); - if (Throwables.getCausalChain(err).stream().filter(e -> e instanceof UnknownHostException).count() > 0) { - responseWrapper.setErrorCode(ErrorEnum.UNKNOWN_HOST_EXCEPTION.getErrorCode()); - responseWrapper.setErrorMessage(err.getMessage()); - } else if (err instanceof AzureRuntimeException) { - responseWrapper.setErrorCode(((AzureRuntimeException) err).getCode()); - responseWrapper.setErrorMessage(err.getMessage()); + try { + return authentication.tenants().listAsync() + .toList() + .toBlocking() + .singleOrDefault(Collections.emptyList()); + } catch (Exception err) { + LOGGER.warning(Throwables.getStackTraceAsString(err)); + if (Throwables.getCausalChain(err).stream().filter(e -> e instanceof UnknownHostException).count() > 0) { + throw new AzureRuntimeException(ErrorEnum.UNKNOWN_HOST_EXCEPTION); + } else if (err instanceof AzureRuntimeException) { + throw err; + } + return Collections.emptyList(); } - return responseWrapper; } protected Azure.Authenticated authTenant(String tenantId) {