Skip to content

Commit

Permalink
fix: dont 'fatal IDE error' when cluster is not reachable (#621)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Nov 23, 2023
1 parent 653a002 commit 3f6c494
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ private CompletableFuture<Odo> getOdo(BiConsumer<Odo, Throwable> 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;
Expand Down Expand Up @@ -126,7 +132,7 @@ public Map<String, ComponentDescriptor> getComponents() {
return components;
}

protected void loadProjectModel(Odo odo, Project project) {
protected void loadProjectModel(Odo odo, Project project) throws IOException {
if (odo == null) {
return;
}
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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));
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3f6c494

Please sign in to comment.