Simplify error handling in account username setting

Move the check for valid account pattern down into AccountSecurityImpl
and raise InvalidUserNameException from there.

Remove the invalidUserName utility method which is no longer needed
as we now only need to generate the error dialog at one place.

Check for the exception being an instance of InvalidUserNameException
rather than doing a string comparison of the message.

Change-Id: Ia4a9df88318f8459e8dc4e63368d896791dd9a18
This commit is contained in:
David Pursehouse 2014-08-20 14:56:16 +09:00
parent f466383995
commit fb4c874509
2 changed files with 11 additions and 15 deletions

View File

@ -106,18 +106,14 @@ class UsernameField extends Composite {
return; return;
} }
enableUI(false);
String newName = userNameTxt.getText(); String newName = userNameTxt.getText();
if ("".equals(newName)) { if ("".equals(newName)) {
newName = null; newName = null;
} }
if (newName != null && !newName.matches(Account.USER_NAME_PATTERN)) {
invalidUserName();
return;
}
enableUI(false);
final String newUserName = newName; final String newUserName = newName;
Util.ACCOUNT_SEC.changeUserName(newUserName, Util.ACCOUNT_SEC.changeUserName(newUserName,
new GerritCallback<VoidResult>() { new GerritCallback<VoidResult>() {
public void onSuccess(final VoidResult result) { public void onSuccess(final VoidResult result) {
@ -131,8 +127,8 @@ class UsernameField extends Composite {
@Override @Override
public void onFailure(final Throwable caught) { public void onFailure(final Throwable caught) {
enableUI(true); enableUI(true);
if (InvalidUserNameException.MESSAGE.equals(caught.getMessage())) { if (caught instanceof InvalidUserNameException) {
invalidUserName(); new ErrorDialog(Util.C.invalidUserName()).center();
} else { } else {
super.onFailure(caught); super.onFailure(caught);
} }
@ -140,10 +136,6 @@ class UsernameField extends Composite {
}); });
} }
private void invalidUserName() {
new ErrorDialog(Util.C.invalidUserName()).center();
}
private void enableUI(final boolean on) { private void enableUI(final boolean on) {
userNameTxt.setEnabled(on); userNameTxt.setEnabled(on);
setUserName.setEnabled(on); setUserName.setEnabled(on);

View File

@ -20,6 +20,7 @@ import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.data.AccountSecurity; import com.google.gerrit.common.data.AccountSecurity;
import com.google.gerrit.common.data.ContributorAgreement; import com.google.gerrit.common.data.ContributorAgreement;
import com.google.gerrit.common.errors.ContactInformationStoreException; import com.google.gerrit.common.errors.ContactInformationStoreException;
import com.google.gerrit.common.errors.InvalidUserNameException;
import com.google.gerrit.common.errors.NoSuchEntityException; import com.google.gerrit.common.errors.NoSuchEntityException;
import com.google.gerrit.common.errors.PermissionDeniedException; import com.google.gerrit.common.errors.PermissionDeniedException;
import com.google.gerrit.httpd.rpc.BaseServiceImplementation; import com.google.gerrit.httpd.rpc.BaseServiceImplementation;
@ -109,10 +110,13 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
public void changeUserName(final String newName, public void changeUserName(final String newName,
final AsyncCallback<VoidResult> callback) { final AsyncCallback<VoidResult> callback) {
if (realm.allowsEdit(Account.FieldName.USER_NAME)) { if (realm.allowsEdit(Account.FieldName.USER_NAME)) {
if (newName == null || !newName.matches(Account.USER_NAME_PATTERN)) {
callback.onFailure(new InvalidUserNameException());
}
Handler.wrap(changeUserNameFactory.create(newName)).to(callback); Handler.wrap(changeUserNameFactory.create(newName)).to(callback);
} else { } else {
callback.onFailure(new PermissionDeniedException("Not allowed to change" callback.onFailure(
+ " username")); new PermissionDeniedException("Not allowed to change username"));
} }
} }