From 737d2819273e7ba07d3a993422b138b93b852415 Mon Sep 17 00:00:00 2001 From: Mohamed Zenadi Date: Sun, 11 Dec 2016 16:57:17 +0100 Subject: Handle jgit errors (#243) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * initial work on the git error handling * remove throws exception and handle the jsch one correctly * move the commit task into its own operation * get rid of the interface and rely on the abstract class GitOperation * add error message to the pull command * add error message to the push command * add error message to the sync operationˆ --- .../java/com/zeapo/pwdstore/git/GitOperation.java | 77 +++++++++++++--------- 1 file changed, 47 insertions(+), 30 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java index 8ec13593..f6b05f4c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.java @@ -10,6 +10,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import com.jcraft.jsch.JSch; +import com.jcraft.jsch.JSchException; import com.jcraft.jsch.KeyPair; import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.UserPreference; @@ -75,10 +76,8 @@ public abstract class GitOperation { /** * Executes the GitCommand in an async task - * - * @throws Exception */ - public abstract void execute() throws Exception; + public abstract void execute(); /** * Executes the GitCommand in an async task after creating the authentication @@ -86,9 +85,8 @@ public abstract class GitOperation { * @param connectionMode the server-connection mode * @param username the username * @param sshKey the ssh-key file - * @throws Exception */ - public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) throws Exception { + public void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey) { executeAfterAuthentication(connectionMode, username, sshKey, false); } @@ -99,9 +97,8 @@ public abstract class GitOperation { * @param username the username * @param sshKey the ssh-key file * @param showError show the passphrase edit text in red - * @throws Exception */ - private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) throws Exception { + private void executeAfterAuthentication(final String connectionMode, final String username, @Nullable final File sshKey, final boolean showError) { if (connectionMode.equalsIgnoreCase("ssh-key")) { if (sshKey == null || !sshKey.exists()) { new AlertDialog.Builder(callingActivity) @@ -152,15 +149,16 @@ public abstract class GitOperation { passphrase.setError("Wrong passphrase"); } JSch jsch = new JSch(); - final KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); - if (keyPair.isEncrypted()) { - new AlertDialog.Builder(callingActivity) - .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) - .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) - .setView(passphrase) - .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - try { + try { + final KeyPair keyPair = KeyPair.load(jsch, callingActivity.getFilesDir() + "/.ssh_key"); + + if (keyPair.isEncrypted()) { + new AlertDialog.Builder(callingActivity) + .setTitle(callingActivity.getResources().getString(R.string.passphrase_dialog_title)) + .setMessage(callingActivity.getResources().getString(R.string.passphrase_dialog_text)) + .setView(passphrase) + .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { if (keyPair.decrypt(passphrase.getText().toString())) { // Authenticate using the ssh-key and then execute the command setAuthentication(sshKey, username, passphrase.getText().toString()).execute(); @@ -168,18 +166,25 @@ public abstract class GitOperation { // call back the method executeAfterAuthentication(connectionMode, username, sshKey, true); } - } catch (Exception e) { - e.printStackTrace(); } + }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + // Do nothing. + } + }).show(); + } else { + setAuthentication(sshKey, username, "").execute(); + } + } catch (JSchException e) { + new AlertDialog.Builder(callingActivity) + .setTitle("Unable to open the ssh-key") + .setMessage("Please check that it was imported.") + .setPositiveButton("Ok", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { } - }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - // Do nothing. - } - }).show(); - } else { - setAuthentication(sshKey, username, "").execute(); + }).show(); } } } else { @@ -195,11 +200,7 @@ public abstract class GitOperation { .setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // authenticate using the user/pwd and then execute the command - try { - setAuthentication(username, password.getText().toString()).execute(); - } catch (Exception e) { - e.printStackTrace(); - } + setAuthentication(username, password.getText().toString()).execute(); } }).setNegativeButton(callingActivity.getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { @@ -209,4 +210,20 @@ public abstract class GitOperation { }).show(); } } + + public void onTaskEnded(String result) { + new AlertDialog.Builder(callingActivity). + setTitle(callingActivity.getResources().getString(R.string.jgit_error_dialog_title)). + setMessage("Error occurred during a Git operation, " + + callingActivity.getResources().getString(R.string.jgit_error_dialog_text) + + result + + "\nPlease check the FAQ for possible reasons why this error might occur."). + setPositiveButton(callingActivity.getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + callingActivity.setResult(Activity.RESULT_CANCELED); + callingActivity.finish(); + } + }).show(); + } } -- cgit v1.2.3