Skip to content

Commit 6bcc138

Browse files
yossibalan-starIgor Vinokur
authored andcommitted
generateExceptionMessage for git actions
Signed-off-by: i053322 <yossi.balan@sap.com>
1 parent f2ef69d commit 6bcc138

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

wsagent/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
import org.slf4j.LoggerFactory;
145145

146146
import javax.inject.Inject;
147+
import javax.net.ssl.SSLHandshakeException;
147148
import java.io.File;
148149
import java.io.FileOutputStream;
149150
import java.io.FilenameFilter;
@@ -230,6 +231,8 @@ class JGitConnection implements GitConnection {
230231
private static final String MESSAGE_COMMIT_NOT_POSSIBLE = "Commit is not possible because repository state is '%s'";
231232
private static final String MESSAGE_COMMIT_AMEND_NOT_POSSIBLE = "Amend is not possible because repository state is '%s'";
232233

234+
private static final String FILE_NAME_TOO_LONG_ERROR_PREFIX = "File name too long";
235+
233236
private static final Logger LOG = LoggerFactory.getLogger(JGitConnection.class);
234237

235238
private Git git;
@@ -521,7 +524,8 @@ protected void onEndTask(String taskName, int workCurr, int workTotal, int perce
521524
}
522525
return;
523526
}
524-
throw new GitException(exception.getMessage(), exception);
527+
String message = generateExceptionMessage(exception);
528+
throw new GitException(message, exception);
525529
}
526530
}
527531

@@ -684,7 +688,7 @@ public void fetch(FetchRequest request) throws GitException, UnauthorizedExcepti
684688
} else if ("Nothing to fetch.".equals(exception.getMessage())) {
685689
return;
686690
} else {
687-
errorMessage = exception.getMessage();
691+
errorMessage = generateExceptionMessage(exception);
688692
}
689693
throw new GitException(errorMessage, exception);
690694
}
@@ -1030,7 +1034,7 @@ public PullResponse pull(PullRequest request) throws GitException, UnauthorizedE
10301034
if (exception.getMessage().equals("Invalid remote: " + remoteName)) {
10311035
errorMessage = ERROR_NO_REMOTE_REPOSITORY;
10321036
} else {
1033-
errorMessage = exception.getMessage();
1037+
errorMessage = generateExceptionMessage(exception);
10341038
}
10351039
throw new GitException(errorMessage, exception);
10361040
}
@@ -1111,7 +1115,8 @@ public PushResponse push(PushRequest request) throws GitException, UnauthorizedE
11111115
if ("origin: not found.".equals(exception.getMessage())) {
11121116
throw new GitException(ERROR_NO_REMOTE_REPOSITORY, exception);
11131117
} else {
1114-
throw new GitException(exception.getMessage(), exception);
1118+
String message = generateExceptionMessage(exception);
1119+
throw new GitException(message, exception);
11151120
}
11161121
}
11171122
}
@@ -1550,7 +1555,8 @@ public boolean accept(File dir) {
15501555
}
15511556
}
15521557
} catch (IOException exception) {
1553-
throw new GitException(exception.getMessage(), exception);
1558+
String message = generateExceptionMessage(exception);
1559+
throw new GitException(message, exception);
15541560
}
15551561
}
15561562

@@ -1708,4 +1714,42 @@ private String cleanRemoteName(String branchName) throws GitException {
17081714
}
17091715
return returnName;
17101716
}
1717+
1718+
/**
1719+
* Method for generate exception message. The default logic return message from the error.
1720+
* It also check if the type of the message is for SSL or in case that the error
1721+
* start with "file name to long" then it raise the relevant message
1722+
*
1723+
* @param error
1724+
* throwable error
1725+
* @return exception message
1726+
*/
1727+
private String generateExceptionMessage(Throwable error) {
1728+
String message = error.getMessage();
1729+
while (error.getCause() != null) {
1730+
//if e caused by an SSLHandshakeException - replace thrown message with a hardcoded message
1731+
if (error.getCause() instanceof SSLHandshakeException) {
1732+
message = "The system is not configured to trust the security certificate provided by the Git server";
1733+
break;
1734+
} else if (error.getCause() instanceof IOException) {
1735+
// Security fix - error message should not include complete local file path on the target system
1736+
// Error message for example - File name too long (path /xx/xx/xx/xx/xx/xx/xx/xx /, working dir /xx/xx/xx)
1737+
if (message != null && message.startsWith(FILE_NAME_TOO_LONG_ERROR_PREFIX)) {
1738+
try {
1739+
String repoPath = repository.getWorkTree().getCanonicalPath();
1740+
int startIndex = message.indexOf(repoPath);
1741+
int endIndex = message.indexOf(",");
1742+
if (startIndex > -1 && endIndex > -1) {
1743+
message = FILE_NAME_TOO_LONG_ERROR_PREFIX + " " + message.substring(startIndex + repoPath.length(), endIndex);
1744+
}
1745+
break;
1746+
} catch (IOException e) {
1747+
//Hide exception as it is only needed for this message generation
1748+
}
1749+
}
1750+
}
1751+
error = error.getCause();
1752+
}
1753+
return message;
1754+
}
17111755
}

0 commit comments

Comments
 (0)