From bb74188ce66b4da1294a45bcdbc92986fe3d7854 Mon Sep 17 00:00:00 2001 From: yuwe Date: Fri, 16 Sep 2022 09:32:33 +0800 Subject: [PATCH] block() function is not allowed to run in nio thread. use subscribe() to get string in non-blocking way. --- .../lib/common/messager/AzureMessage.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/messager/AzureMessage.java b/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/messager/AzureMessage.java index 7dd4aace43..976aaad944 100644 --- a/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/messager/AzureMessage.java +++ b/azure-toolkit-libs/azure-toolkit-common-lib/src/main/java/com/microsoft/azure/toolkit/lib/common/messager/AzureMessage.java @@ -39,6 +39,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -105,19 +106,20 @@ protected String getCause(@Nonnull Throwable throwable) { if (Objects.isNull(root)) { return ExceptionUtils.getRootCause(throwable).toString(); } - String cause = null; + AtomicReference cause = new AtomicReference<>(null); if (root instanceof ManagementException) { - cause = Optional.of((ManagementException) root) - .map(ManagementException::getValue) - .map(ManagementError::getMessage) - .orElse("Unknown cause"); + cause.set(Optional.of((ManagementException) root) + .map(ManagementException::getValue) + .map(ManagementError::getMessage) + .orElse("Unknown cause")); } else if (root instanceof HttpResponseException) { - cause = Optional.of((HttpResponseException) root) + Optional.of((HttpResponseException) root) .map(HttpResponseException::getResponse) .map(HttpResponse::getBodyAsString) - .map(Mono::block).orElse("Unknown cause"); + .orElse(Mono.just("Unknown cause")) + .subscribe(cause::set); } - final String causeMsg = StringUtils.firstNonBlank(cause, root.getMessage()); + final String causeMsg = StringUtils.firstNonBlank(cause.get(), root.getMessage()); return Optional.ofNullable(causeMsg) .filter(StringUtils::isNotBlank) .map(StringUtils::uncapitalize)