1

Below code is of git push command through java everything if I run without using try catch block it successfully pushing the files, But im not getting that how to throw error if user enter wrong url and username and password by using try and catch block can anyone do correct edit in the code for it.

  public void pushRepo (String repoUrl, String gitdirectory, String username, String password) throws GitAPIException
{

      Git git = null;
    try {
        git = Git.open(new File(gitdirectory));
    } catch (IOException e1) {
        System.out.println("it is not git directory");
    }
      RemoteAddCommand remoteAddCommand = git.remoteAdd();
      remoteAddCommand.setName("origin");
      try {
      remoteAddCommand.setUri(new URIish(repoUrl));
      System.out.println("file added");
      }catch (Exception e) {
           System.out.println("Invalid RemoteUrl");
        }
      remoteAddCommand.call();
      git.add().addFilepattern(".").call();
      git.commit().setMessage("commited").call();
      PushCommand pushCommand = git.push();
      try {
      pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
      pushCommand.setRemote("origin").add("master").call();
      System.out.println("push file");
      }catch(Exception e)
      {
          System.out.println("Invalid username and password");
      }

    }
}

If I enter value incorrect of any of url ,username and password it always give the message like "invalid username and password."

NikitaJ
  • 53
  • 1
  • 7
  • Best Way to do this would make a custom validator and throw it. – parlad Mar 24 '18 at 13:17
  • Possible duplicate of [Can I catch multiple Java exceptions in the same catch clause?](https://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause) – aaron Mar 24 '18 at 16:27
  • The key question is: why you want user to configure the Git remote URL and credentials multiple times (at each commit)? [Git can save username and password](https://stackoverflow.com/questions/35942754/how-to-save-username-and-password-in-git) – Mincong Huang Mar 24 '18 at 17:39
  • @MincongHuang I want to provide validation – NikitaJ Mar 25 '18 at 07:06

1 Answers1

4

Try to make custom validator

public class ValidationErrorException extends Exception {

private List<String> errors;

public ValidationErrorException(List<String> errors) {
    super("Validation Errors.");
    setErrors(errors);
}

public ValidationErrorException(String message, List<String> errors) {
    super(message);
    setErrors(errors);
}

public void setErrors(List<String> errors) {
    this.errors = errors;
}

public List<String> getErrors() {
    return errors;
  }
 }

Add in your function.

throws ValidationErrorException

Then use

try {
        // write code for check url
    } catch (Exception ex) {
        List<String> errors = new ArrayList<>();
        errors.add("Invalid URL...");
        throw new ValidationErrorException(errors);
    }

And so on for username and password.

parlad
  • 1,143
  • 4
  • 23
  • 42
  • thank u so much for your ans sir I know it will really help me to solve the issues but can you please do edit in my code ,please. – NikitaJ Mar 24 '18 at 13:51
  • because im unable to understand i have never make use of it before – NikitaJ Mar 24 '18 at 13:57
  • we in this community are not supposed to do that , but find me on chat i will do that for you, Because this could help many others , not only your code – parlad Mar 24 '18 at 13:58