Skip to content

Commit 0412a12

Browse files
committed
Further fix for tsantalis#963
1 parent 60ca1fa commit 0412a12

File tree

4 files changed

+78
-33
lines changed

4 files changed

+78
-33
lines changed

src/main/java/gui/webdiff/DiffDriver.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.beust.jcommander.JCommander;
44
import com.beust.jcommander.Parameter;
5+
import org.refactoringminer.exceptions.NetworkException;
56
import gui.webdiff.export.WebExporter;
67
import org.refactoringminer.astDiff.models.ASTDiff;
78
import org.refactoringminer.astDiff.models.ProjectASTDiff;
@@ -48,18 +49,14 @@ public class DiffDriver {
4849
To export the mappings/actions, add --export to the end of the command.
4950
""";
5051

51-
public ProjectASTDiff getProjectASTDiff() {
52+
public ProjectASTDiff getProjectASTDiff() throws Exception {
5253
RunMode runMode = RunMode.getRunMode(this);
5354
if (runMode == null) return null;
5455
ProjectASTDiff projectASTDiff;
55-
try {
56-
projectASTDiff = runMode.getProjectASTDIFF(this);
57-
} catch (Exception e) {
58-
throw new RuntimeException(e);
59-
}
56+
projectASTDiff = runMode.getProjectASTDIFF(this);
6057
return projectASTDiff;
6158
}
62-
public void execute(String[] args) {
59+
public void execute(String[] args) throws Exception {
6360
JCommander jCommander = JCommander.newBuilder()
6461
.addObject(this)
6562
.build();
@@ -83,13 +80,32 @@ public void execute(String[] args) {
8380
else
8481
webDiff.openInBrowser();
8582
}
86-
} catch (org.kohsuke.github.HttpException e) {
87-
System.out.println("Error in connecting. Please retry");
88-
throw new RuntimeException(e);
8983
} catch (Exception e) {
90-
System.out.println(HELP_MSG);
91-
throw new RuntimeException(e);
84+
if (isConnectionIssue(e))
85+
throw new NetworkException();
86+
else
87+
{
88+
System.out.println(HELP_MSG);
89+
throw e;
90+
}
91+
}
92+
}
93+
94+
private static boolean isConnectionIssue(Throwable e) {
95+
while (e != null) {
96+
if (e instanceof java.net.ConnectException ||
97+
e instanceof java.nio.channels.UnresolvedAddressException ||
98+
(e.getMessage() != null && (
99+
e.getMessage().contains("api.github.com") ||
100+
e.getMessage().contains("Failed to connect") ||
101+
e.getMessage().contains("Connection refused") ||
102+
e.getMessage().contains("Network is unreachable")
103+
))) {
104+
return true;
105+
}
106+
e = e.getCause();
92107
}
108+
return false;
93109
}
94110

95111
public static void export(ProjectASTDiff projectASTDiff, String exportDestination) throws IOException {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.refactoringminer.exceptions;
2+
3+
public class NetworkException extends RuntimeException {
4+
5+
private static final String DEFAULT_MESSAGE = "❌ Error in connecting. Please retry.";
6+
7+
public NetworkException() {
8+
super(DEFAULT_MESSAGE);
9+
}
10+
public NetworkException(String msg){
11+
super(msg);
12+
}
13+
14+
public NetworkException(Throwable cause) {
15+
super(DEFAULT_MESSAGE, cause);
16+
}
17+
18+
public NetworkException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.refactoringminer.exceptions;
2+
3+
public class OAuthTokenNotFoundException extends NetworkException {
4+
private static final String DEFAULT_MESSAGE =
5+
"❌ Missing OAuth token. Please set the environment variable 'OAuthToken' with your personal access token.";
6+
7+
public OAuthTokenNotFoundException() {
8+
super(DEFAULT_MESSAGE);
9+
}
10+
11+
public OAuthTokenNotFoundException(Exception e) {
12+
super(DEFAULT_MESSAGE, e);
13+
}
14+
}

src/main/java/org/refactoringminer/rm1/GitHistoryRefactoringMinerImpl.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.io.File;
1212
import java.io.FileInputStream;
13-
import java.io.FileNotFoundException;
1413
import java.io.FileOutputStream;
1514
import java.io.IOException;
1615
import java.io.InputStream;
@@ -71,6 +70,7 @@
7170
import org.refactoringminer.astDiff.models.ProjectASTDiff;
7271
import org.refactoringminer.astDiff.utils.URLHelper;
7372
import org.refactoringminer.astDiff.matchers.ProjectASTDiffer;
73+
import org.refactoringminer.exceptions.OAuthTokenNotFoundException;
7474
import org.refactoringminer.util.GitServiceImpl;
7575
import org.slf4j.Logger;
7676
import org.slf4j.LoggerFactory;
@@ -729,14 +729,16 @@ private GitHub connectToGitHub() {
729729
if(gitHub.isCredentialValid()) {
730730
logger.info("Connected to GitHub with OAuth token");
731731
}
732+
else {
733+
throw new OAuthTokenNotFoundException();
734+
}
732735
}
733736
else {
734737
gitHub = GitHub.connect();
735738
}
736-
} catch(FileNotFoundException e) {
737-
logger.warn("File github-oauth.properties was not found in RefactoringMiner's execution directory", e);
738-
} catch(IOException ioe) {
739-
ioe.printStackTrace();
739+
}
740+
catch (Exception e){
741+
throw new OAuthTokenNotFoundException(e);
740742
}
741743
}
742744
return gitHub;
@@ -945,10 +947,8 @@ public void detectAtCommit(Repository repository, String commitId, RefactoringHa
945947
f.get(timeout, TimeUnit.SECONDS);
946948
} catch (TimeoutException e) {
947949
f.cancel(true);
948-
} catch (ExecutionException e) {
949-
e.printStackTrace();
950-
} catch (InterruptedException e) {
951-
e.printStackTrace();
950+
} catch (ExecutionException | InterruptedException e) {
951+
throw new RuntimeException(e);
952952
} finally {
953953
service.shutdown();
954954
}
@@ -997,10 +997,8 @@ public void detectAtCommit(String gitURL, String commitId, RefactoringHandler ha
997997
f.get(timeout, TimeUnit.SECONDS);
998998
} catch (TimeoutException e) {
999999
f.cancel(true);
1000-
} catch (ExecutionException e) {
1001-
e.printStackTrace();
1002-
} catch (InterruptedException e) {
1003-
e.printStackTrace();
1000+
} catch (ExecutionException | InterruptedException e) {
1001+
throw new RuntimeException(e);
10041002
} finally {
10051003
service.shutdown();
10061004
}
@@ -1902,10 +1900,8 @@ public ProjectASTDiff diffAtPullRequest(String cloneURL, int pullRequestId, int
19021900
f.get(timeout, TimeUnit.SECONDS);
19031901
} catch (TimeoutException e) {
19041902
f.cancel(true);
1905-
} catch (ExecutionException e) {
1906-
e.printStackTrace();
1907-
} catch (InterruptedException e) {
1908-
e.printStackTrace();
1903+
} catch (ExecutionException | InterruptedException e) {
1904+
throw new RuntimeException(e);
19091905
} finally {
19101906
service.shutdown();
19111907
}
@@ -2078,10 +2074,8 @@ public ProjectASTDiff diffAtCommit(String gitURL, String commitId, int timeout)
20782074
f.get(timeout, TimeUnit.SECONDS);
20792075
} catch (TimeoutException e) {
20802076
f.cancel(true);
2081-
} catch (ExecutionException e) {
2082-
e.printStackTrace();
2083-
} catch (InterruptedException e) {
2084-
e.printStackTrace();
2077+
} catch (ExecutionException | InterruptedException e) {
2078+
throw new RuntimeException(e);
20852079
} finally {
20862080
service.shutdown();
20872081
}

0 commit comments

Comments
 (0)