Skip to content

Commit

Permalink
Update error handling for connection through proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Sep 26, 2023
1 parent 3b3e7b3 commit 62de99a
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class ToolUtil {
public static final String CLI_HELP_FILE_PREFIX = "dist-";
private static final String BALLERINA_1_X_VERSIONS = "1.0.";
private static final String CONNECTION_ERROR_MESSAGE = "connection to the remote server failed";
private static final String PROXY_ERROR_MESSAGE = "connection to the remote server through proxy server failed";
private static final String BALLERINA_SETTINGS_FILE = "Settings.toml";
public static final boolean BALLERINA_STAGING_UPDATE = Boolean.parseBoolean(
System.getenv("BALLERINA_STAGING_UPDATE"));
Expand Down Expand Up @@ -242,6 +243,16 @@ public static Map<String, Object> getProxyConfigs () {
return proxyConfigs;
}

private static boolean checkProxyConfigsDefinition() {
File settingsFile = new File(OSUtils.getBallerinaHomePath() + File.separator +
BALLERINA_SETTINGS_FILE );
if (settingsFile.exists()) {
Toml toml = new Toml().read(settingsFile);
return toml.contains("proxy");
}
return false;
}

public static List<Channel> getDistributions(PrintStream printStream) {
HttpsURLConnection conn = null;
List<Channel> channels = new ArrayList<>();
Expand Down Expand Up @@ -302,7 +313,11 @@ public static List<Channel> getDistributions(PrintStream printStream) {
}
}
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down Expand Up @@ -330,7 +345,11 @@ public static String getLatest(String currentVersion, String type) {
}
throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn));
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down Expand Up @@ -376,7 +395,11 @@ public static Tool getLatestToolVersion() {
}
throw ErrorUtil.createCommandException(getServerRequestFailedErrorMessage(conn));
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down Expand Up @@ -517,7 +540,11 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
return true;
}
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down Expand Up @@ -596,7 +623,11 @@ public static void getDependency(PrintStream printStream, String distribution, S
}
}
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down Expand Up @@ -626,7 +657,11 @@ private static void downloadDependency(PrintStream printStream, String dependenc
throw ErrorUtil.createDependencyNotFoundException(dependency);
}
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
}
}

Expand Down Expand Up @@ -668,7 +703,11 @@ public static void downloadTool(PrintStream printStream, String toolVersion) {
throw ErrorUtil.createCommandException("tool version '" + toolVersion + "' not found ");
}
} catch (IOException e) {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
if (checkProxyConfigsDefinition()) {
throw ErrorUtil.createCommandException(PROXY_ERROR_MESSAGE);
} else {
throw ErrorUtil.createCommandException(CONNECTION_ERROR_MESSAGE);
}
} finally {
if (conn != null) {
conn.disconnect();
Expand Down

0 comments on commit 62de99a

Please sign in to comment.