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

@@ -20,6 +20,7 @@ import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.data.AccountSecurity;
import com.google.gerrit.common.data.ContributorAgreement;
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.PermissionDeniedException;
import com.google.gerrit.httpd.rpc.BaseServiceImplementation;
@@ -109,10 +110,13 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
public void changeUserName(final String newName,
final AsyncCallback<VoidResult> callback) {
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);
} else {
callback.onFailure(new PermissionDeniedException("Not allowed to change"
+ " username"));
callback.onFailure(
new PermissionDeniedException("Not allowed to change username"));
}
}