Improvements to error handling for CF dynamic values

This commit is contained in:
nsingh
2017-01-18 15:27:18 -08:00
parent 7849516ca1
commit 86501b0889
6 changed files with 72 additions and 32 deletions

View File

@@ -75,11 +75,11 @@ public class CFTarget {
return params;
}
public List<CFBuildpack> getBuildpacks() throws ExecutionException {
public List<CFBuildpack> getBuildpacks() throws Exception {
return this.buildpacksCache.get(getName());
}
public List<CFServiceInstance> getServices() throws ExecutionException {
public List<CFServiceInstance> getServices() throws Exception {
return this.servicesCache.get(getName());
}
@@ -95,4 +95,5 @@ public class CFTarget {
public String toString() {
return "CFClientTarget [params=" + params + ", targetName=" + targetName + "]";
}
}

View File

@@ -52,10 +52,12 @@ public class CFTargetCache {
/**
* @return non-null list of targets, or throws exception if no targets found
* @throws NoTargetsException
* if no targets found
* @throws Exception
* if no targets found, or error in resolving targets
* for any other error encountered
*/
public synchronized List<CFTarget> getOrCreate() throws Exception {
public synchronized List<CFTarget> getOrCreate() throws NoTargetsException, Exception {
List<CFClientParams> allParams = paramsProvider.getParams();
List<CFTarget> targets = new ArrayList<>();
@@ -88,4 +90,6 @@ public class CFTargetCache {
return cfApiUrl;
}
}
}

View File

@@ -15,6 +15,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.springframework.ide.vscode.commons.util.StringUtil;
@@ -42,39 +43,43 @@ public class CfCliParamsProvider implements ClientParamsProvider {
* ClientParamsProvider#getParams()
*/
@Override
public List<CFClientParams> getParams() throws Exception {
File file = getConfigJsonFile();
public List<CFClientParams> getParams() throws NoTargetsException, ExecutionException {
List<CFClientParams> params = new ArrayList<>();
if (file != null) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> userData = mapper.readValue(file, Map.class);
if (userData != null) {
String refreshToken = (String) userData.get(REFRESH_TOKEN);
// Only support connecting to CF via refresh token for now
if (isRefreshTokenSet(refreshToken)) {
CFCredentials credentials = CFCredentials.fromRefreshToken(refreshToken);
boolean sslDisabled = (Boolean) userData.get(SSL_DISABLED);
String target = (String) userData.get(TARGET);
Map<String, Object> orgFields = (Map<String, Object>) userData.get(ORGANIZATION_FIELDS);
Map<String, Object> spaceFields = (Map<String, Object>) userData.get(SPACE_FIELDS);
if (target != null && orgFields != null && spaceFields != null) {
String orgName = (String) orgFields.get(NAME);
String spaceName = (String) spaceFields.get(NAME);
params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled));
try {
File file = getConfigJsonFile();
if (file != null) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> userData = mapper.readValue(file, Map.class);
if (userData != null) {
String refreshToken = (String) userData.get(REFRESH_TOKEN);
// Only support connecting to CF via refresh token for now
if (isRefreshTokenSet(refreshToken)) {
CFCredentials credentials = CFCredentials.fromRefreshToken(refreshToken);
boolean sslDisabled = (Boolean) userData.get(SSL_DISABLED);
String target = (String) userData.get(TARGET);
Map<String, Object> orgFields = (Map<String, Object>) userData.get(ORGANIZATION_FIELDS);
Map<String, Object> spaceFields = (Map<String, Object>) userData.get(SPACE_FIELDS);
if (target != null && orgFields != null && spaceFields != null) {
String orgName = (String) orgFields.get(NAME);
String spaceName = (String) spaceFields.get(NAME);
params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled));
}
}
}
}
} catch (IOException | InterruptedException e) {
throw new ExecutionException(e);
}
if (params.isEmpty()) {
throw new Exception(
throw new NoTargetsException(
"Unable to fetch information from Cloud Foundry. Please use cf CLI to configure and login to Cloud Foundry.");
} else {
return params;
}
}
private boolean isRefreshTokenSet(String token) {
return StringUtil.hasText(token);
}

View File

@@ -11,15 +11,15 @@
package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;
import java.util.List;
import java.util.concurrent.ExecutionException;
public interface ClientParamsProvider {
/**
*
* @return non-null list of VALID params to connect to Cloud Foundry
* @throws Exception if failure to resolve any params for Cloud Foundry
* @throws NoTargetsException if failure to resolve any params for Cloud Foundry
* @throws ExecutionException if failure occurs while resolving params
*/
List<CFClientParams> getParams() throws Exception;
List<CFClientParams> getParams() throws NoTargetsException, ExecutionException;
}

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;
public class NoTargetsException extends Exception {
public NoTargetsException(String message) {
super(message);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}

View File

@@ -21,6 +21,7 @@ import javax.inject.Provider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTarget;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.NoTargetsException;
import org.springframework.ide.vscode.commons.util.Assert;
import org.springframework.ide.vscode.commons.util.ExceptionUtil;
import org.springframework.ide.vscode.commons.yaml.schema.BasicYValueHint;
@@ -50,12 +51,17 @@ public abstract class AbstractCFHintsProvider implements Provider<Collection<YVa
hints.addAll(resolvedHints);
} catch (Throwable e) {
logger.log(Level.SEVERE, e.getMessage(), e);
// Don't propagate exception as to allow the CA to be displayed to the
// Don't propagate exception as to allow the CA to be displayed to
// the
// user.
if (ExceptionUtil.getThrowable(e, IOException.class) != null) {
hints.add(new BasicYValueHint(EMPTY_VALUE, "Connection failure. " + e.getMessage()));
} else {
if (ExceptionUtil.getThrowable(e, NoTargetsException.class) != null) {
hints.add(new BasicYValueHint(EMPTY_VALUE, e.getMessage()));
} else if (ExceptionUtil.getThrowable(e, IOException.class) != null) {
hints.add(new BasicYValueHint(EMPTY_VALUE,
"Connection failure to Cloud Foundry. Please check the log for more details."));
} else {
hints.add(new BasicYValueHint(EMPTY_VALUE,
"Failed to fetch values from Cloud Foundry. Please check the log for more details."));
}
}
return hints;