Fixed regression in error handling in CF support

This commit is contained in:
nsingh
2017-01-17 10:29:59 -08:00
parent eec7f0423f
commit f25d2c9c94
8 changed files with 41 additions and 67 deletions

View File

@@ -8,10 +8,8 @@
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.cloudfoundry.client.v2;
package org.springframework.ide.vscode.commons.cloudfoundry.client;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams;
public interface CloudFoundryClientFactory {

View File

@@ -12,11 +12,10 @@ package org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.util.Assert;
import com.google.common.cache.CacheBuilder;
@@ -53,7 +52,8 @@ public class CFTargetCache {
/**
* @return non-null list of targets, or throws exception if no targets found
* @throws Exception if no targets found, or error in resolving targets
* @throws Exception
* if no targets found, or error in resolving targets
*/
public synchronized List<CFTarget> getOrCreate() throws Exception {
@@ -68,10 +68,6 @@ public class CFTargetCache {
}
}
if (targets.isEmpty()) {
throw new ExecutionException(new Error(paramsProvider.noParamsAvailableMessage()));
}
return targets;
}

View File

@@ -15,11 +15,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -38,8 +33,6 @@ public class CfCliParamsProvider implements ClientParamsProvider {
public static final String NAME = "Name";
public static final String SSL_DISABLED = "SSLDisabled";
private static Logger logger = Logger.getLogger(CfCliParamsProvider.class.getName());
/*
* (non-Javadoc)
*
@@ -47,48 +40,38 @@ public class CfCliParamsProvider implements ClientParamsProvider {
* ClientParamsProvider#getParams()
*/
@Override
public List<CFClientParams> getParams() {
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 (refreshToken == null) {
return null;
}
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);
List<CFClientParams> params = new ArrayList<>();
params.add(new CFClientParams(target, null, credentials, orgName, spaceName, sslDisabled));
return params;
}
public List<CFClientParams> getParams() throws Exception {
File file = getConfigJsonFile();
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 (refreshToken == null) {
return null;
}
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) {
log(e);
}
return null;
}
/*
* (non-Javadoc)
*
* @see org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.
* ClientParamsProvider#noParamsAvailableMessage()
*/
@Override
public String noParamsAvailableMessage() {
return "Unable to fetch information from Cloud Foundry. Please use cf CLI to configure and login to Cloud Foundry.";
if (params.isEmpty()) {
throw new Exception(
"Unable to fetch information from Cloud Foundry. Please use cf CLI to configure and login to Cloud Foundry.");
} else {
return params;
}
}
private File getConfigJsonFile() throws IOException, InterruptedException {
@@ -112,9 +95,4 @@ public class CfCliParamsProvider implements ClientParamsProvider {
private String getUnixHomeDir() throws IOException, InterruptedException {
return System.getProperty("user.home");
}
private void log(Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}

View File

@@ -14,8 +14,11 @@ import java.util.List;
public interface ClientParamsProvider {
List<CFClientParams> getParams();
String noParamsAvailableMessage();
/**
*
* @return non-null list of params to connect to Cloud Foundry
* @throws Exception if failure to resolve any params for Cloud Foundry
*/
List<CFClientParams> getParams() throws Exception;
}

View File

@@ -12,6 +12,7 @@ package org.springframework.ide.vscode.commons.cloudfoundry.client.v2;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientRequests;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFClientParams;
public class DefaultCloudFoundryClientFactoryV2 implements CloudFoundryClientFactory {

View File

@@ -20,7 +20,6 @@ import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTar
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfCliParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ClientParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.DefaultCloudFoundryClientFactoryV2;
public class CFClientTest {

View File

@@ -55,8 +55,7 @@ public abstract class AbstractCFHintsProvider implements Provider<Collection<YVa
if (e instanceof IOException || ExceptionUtil.getDeepestCause(e) instanceof IOException) {
hints.add(new BasicYValueHint(EMPTY_VALUE, "Connection failure. " + e.getMessage()));
} else {
hints.add(new BasicYValueHint(EMPTY_VALUE,
"Unable to fetch Cloud Foundry proposals due to: " + e.getMessage()));
hints.add(new BasicYValueHint(EMPTY_VALUE, e.getMessage()));
}
}
return hints;

View File

@@ -21,8 +21,8 @@ import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CfCliParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.ClientParamsProvider;
import org.springframework.ide.vscode.commons.cloudfoundry.client.ClientTimeouts;
import org.springframework.ide.vscode.commons.cloudfoundry.client.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.cftarget.CFTargetCache;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.CloudFoundryClientFactory;
import org.springframework.ide.vscode.commons.cloudfoundry.client.v2.DefaultCloudFoundryClientFactoryV2;
import org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngine;
import org.springframework.ide.vscode.commons.languageserver.completion.VscodeCompletionEngineAdapter;