【发布时间】:2018-05-21 12:10:35
【问题描述】:
我正在用 Java 和 jGit 创建一个应用程序(继续how to authorize an user using jGit)。目前我能够隔离异常,如果用户提供无效凭据,我会得到not authorized,如果用户无权访问声明的 repo,我会得到repository not found。现在我的问题是,用户正在通过身份验证,因为用户无权访问存储库,因此引发了第二个异常。但是在这里我想在另一个类中处理它,即我希望这个类只处理用户的身份验证。下面是我的代码
import java.io.File;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class AuthenticateanUser {
public static void main(String[] args) throws Exception {
final String REMOTE_URL = "https://myRepo.git";
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("TestGitRepository", "");
localPath.delete();
// then clone
try (Git result = Git.cloneRepository().setURI(REMOTE_URL)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("myId", "myPwd"))
.setDirectory(localPath).call()) {
System.out.println("Having repository: " + result.status());
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("repository not found"))
System.out.println("Exception Repo not found");
else if (e.getMessage().toLowerCase().contains("not authorized"))
System.out.println("Exception Invalid Credentials");
else
System.out.println(e);
}
}
}
请让我知道我哪里出错了,我该如何解决这个问题。
谢谢
【问题讨论】:
标签: java exception-handling jgit