diff --git a/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsRootNode.java b/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsRootNode.java index 4cadcc9bb..6db14106d 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsRootNode.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsRootNode.java @@ -82,7 +82,13 @@ private CompletableFuture getOdo(BiConsumer whenComplete) { this.odoFuture = ToolFactory.getInstance() .createOdo(project) .thenApply(odo -> (Odo) new ApplicationRootNodeOdo(odo, this)) - .whenComplete((odo, err) -> loadProjectModel(odo, project)) + .whenComplete((odo, err) -> { + try { + loadProjectModel(odo, project); + } catch (IOException e) { + + } + }) .whenComplete(whenComplete); } return odoFuture; @@ -126,7 +132,7 @@ public Map getComponents() { return components; } - protected void loadProjectModel(Odo odo, Project project) { + protected void loadProjectModel(Odo odo, Project project) throws IOException { if (odo == null) { return; } @@ -137,7 +143,11 @@ protected void loadProjectModel(Odo odo, Project project) { @Override public void moduleAdded(@NotNull Project project, @NotNull Module module) { - addContext(getOdo().getNow(null), ProjectUtils.getModuleRoot(module)); + try { + addContext(getOdo().getNow(null), ProjectUtils.getModuleRoot(module)); + } catch (IOException e) { + throw new RuntimeException(e); + } } @Override @@ -171,7 +181,7 @@ private void migrateOdo(String path, ComponentDescriptor descriptor) { project)); } - private void addContext(Odo odo, VirtualFile modulePathFile) { + private void addContext(Odo odo, VirtualFile modulePathFile) throws IOException { if (odo == null) { return; } @@ -183,21 +193,21 @@ private void addContext(Odo odo, VirtualFile modulePathFile) { ); } catch (IOException ex) { //filter out some common exception when no logged or no authorizations - if (doNotLogFromMessage(ex.getMessage())) { - LOGGER.error(ex.getLocalizedMessage(), ex); + if (shouldLogMessage(ex.getMessage())) { + LOGGER.warn(ex.getLocalizedMessage(), ex); } } } } - private static boolean doNotLogFromMessage(String message) { + private static boolean shouldLogMessage(String message) { return !(message.contains("Unauthorized") || message.contains("unable to access the cluster: servicebindings.binding.operators.coreos.com") || message.contains("the server has asked for the client to provide credentials") || message.contains("connect: no route to host")); } - public void addContext(String modulePath) { + public void addContext(String modulePath) throws IOException { addContext(getOdo().getNow(null), LocalFileSystem.getInstance().refreshAndFindFileByPath(modulePath)); } @@ -233,9 +243,8 @@ public void onUpdate(ConfigWatcher source, Config config) { public synchronized void refresh() { resetOdo(); - CompletableFuture - .runAsync(() -> - getOdo((odo, err) -> structure.fireModified(ApplicationsRootNode.this)), + getOdo((odo, err) -> structure.fireModified(ApplicationsRootNode.this)) + .whenCompleteAsync((Odo odo, Throwable err) -> System.err.println(err), SwingUtils.EXECUTOR_BACKGROUND); } diff --git a/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java b/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java index 9680bbb74..8b238f37a 100644 --- a/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java +++ b/src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java @@ -20,6 +20,7 @@ import com.redhat.devtools.intellij.common.tree.MutableModelSupport; import io.fabric8.kubernetes.client.KubernetesClientException; import org.jboss.tools.intellij.openshift.Constants; +import org.jboss.tools.intellij.openshift.utils.ExceptionUtils; import org.jboss.tools.intellij.openshift.utils.helm.Helm; import org.jboss.tools.intellij.openshift.utils.odo.Odo; import org.jetbrains.annotations.NotNull; @@ -68,23 +69,28 @@ public Object getApplicationsRoot() { @NotNull @Override public Object @NotNull [] getChildElements(@NotNull Object element) { - Odo odo = root.getOdo().getNow(null); - if (odo != null) { - if (element == this) { - return new Object[]{getApplicationsRoot(), registries}; - } else if (element instanceof ApplicationsRootNode) { - return getCurrentNamespace((ApplicationsRootNode) element, odo); - } else if (element instanceof NamespaceNode) { - return createNamespaceChildren((NamespaceNode) element, odo); - } else if (element instanceof ComponentNode) { - return createComponentChildren((ComponentNode) element, odo); - } else if (element instanceof DevfileRegistriesNode) { - return getRegistries(root, odo); - } else if (element instanceof DevfileRegistryNode) { - return getRegistryComponentTypes((DevfileRegistryNode) element, odo); - } else if (element instanceof DevfileRegistryComponentTypeNode) { - return getRegistryComponentTypeStarters((DevfileRegistryComponentTypeNode) element, odo); + if (element == this) { + return new Object[]{root, registries}; + } + try { + Odo odo = root.getOdo().getNow(null); + if (odo != null) { + if (element instanceof ApplicationsRootNode) { + return getCurrentNamespace((ApplicationsRootNode) element, odo); + } else if (element instanceof NamespaceNode) { + return createNamespaceChildren((NamespaceNode) element, odo); + } else if (element instanceof ComponentNode) { + return createComponentChildren((ComponentNode) element, odo); + } else if (element instanceof DevfileRegistriesNode) { + return getRegistries(root, odo); + } else if (element instanceof DevfileRegistryNode) { + return getRegistryComponentTypes((DevfileRegistryNode) element, odo); + } else if (element instanceof DevfileRegistryComponentTypeNode) { + return getRegistryComponentTypeStarters((DevfileRegistryComponentTypeNode) element, odo); + } } + } catch (Exception e) { + return new Object[]{new MessageNode<>(root, root, ExceptionUtils.getMessage(e))}; } return new Object[0]; } @@ -142,7 +148,7 @@ private Object[] getCurrentNamespace(ApplicationsRootNode element, @NotNull Odo return namespaces.toArray(); } - private static MessageNode createErrorNode(ApplicationsRootNode element, Exception e) { + private MessageNode createErrorNode(ApplicationsRootNode element, Exception e) { if (e instanceof KubernetesClientException) { KubernetesClientException kce = (KubernetesClientException) e; if (kce.getCode() == 401) { diff --git a/src/main/java/org/jboss/tools/intellij/openshift/utils/ExceptionUtils.java b/src/main/java/org/jboss/tools/intellij/openshift/utils/ExceptionUtils.java new file mode 100644 index 000000000..dfc42a2f4 --- /dev/null +++ b/src/main/java/org/jboss/tools/intellij/openshift/utils/ExceptionUtils.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.intellij.openshift.utils; + +import java.util.concurrent.CompletionException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ExceptionUtils { + + private static final Pattern PATTERN_COMPLETION_EXCEPTION_MESSAGE = Pattern.compile( + "\\{[^\"]+\"message\": \"(.+)\"[^}]+}", Pattern.MULTILINE); + private ExceptionUtils() {} + + public static String getMessage(Exception e) { + if (e instanceof CompletionException) { + return getMessage((CompletionException) e); + } else { + return e.getMessage(); + } + } + + public static String getMessage(CompletionException e) { + if (e.getMessage() == null) { + return null; + } + Matcher matcher = PATTERN_COMPLETION_EXCEPTION_MESSAGE.matcher(e.getMessage()); + if (!matcher.find()) { + return e.getMessage(); + } + return matcher.group(1); + } +}