Skip to content

Commit be345c7

Browse files
committed
More explicit exception
1 parent 86d9081 commit be345c7

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,15 @@ public List<Long> getSecurityGroupIdList() {
121121
public void execute() {
122122
try {
123123
UserVm userVm = _userVmService.moveVmToUser(this);
124-
if (userVm == null) {
125-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm");
126-
}
127124
UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", userVm).get(0);
128125
response.setResponseName(getCommandName());
129126
setResponseObject(response);
130-
} catch (InvalidParameterValueException e){
131-
e.printStackTrace();
132-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
133127
} catch (Exception e) {
134-
logger.error("Failed to move vm due to: " + e.getStackTrace());
135-
if (e.getMessage() != null) {
136-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm due to " + e.getMessage());
137-
} else if (e.getCause() != null) {
138-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm due to " + e.getCause());
139-
} else {
140-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm");
141-
}
128+
ApiErrorCode errorCode = e instanceof InvalidParameterValueException ? ApiErrorCode.PARAM_ERROR : ApiErrorCode.INTERNAL_ERROR;
129+
String msg = String.format("Failed to move VM [%s].", getVmId());
130+
logger.error(msg, e);
131+
throw new ServerApiException(errorCode, msg);
142132
}
143-
144133
}
145134

146135
@Override

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7450,9 +7450,7 @@ protected void validateIfVmSupportsMigration(UserVmVO vm, Long vmId) {
74507450
if (vm == null) {
74517451
throw new InvalidParameterValueException(String.format("There is no VM by ID [%s].", vmId));
74527452
} else if (vm.getState() == State.Running) {
7453-
String errMsg = String.format("Unable to move VM [%s] in [%s] state.", vm, vm.getState());
7454-
logger.warn(errMsg);
7455-
throw new InvalidParameterValueException(errMsg);
7453+
throw new InvalidParameterValueException(String.format("Unable to move VM [%s] in [%s] state.", vm, vm.getState()));
74567454
} else if (UserVmManager.SHAREDFSVM.equals(vm.getUserVmType())) {
74577455
throw new InvalidParameterValueException("Migration is not supported for Shared FileSystem Instances.");
74587456
}
@@ -7527,13 +7525,19 @@ protected void verifyResourceLimitsForAccountAndStorage(Account account, UserVmV
75277525
}
75287526

75297527
protected void validateIfNewOwnerHasAccessToTemplate(UserVmVO vm, Account newAccount, VirtualMachineTemplate template) {
7530-
logger.trace(String.format("Validating if new owner [%s] has access to the template specified for VM [%s].", newAccount, vm));
7528+
logger.trace("Validating if new owner [{}] has access to the template specified for VM [{}].", newAccount, vm);
75317529

75327530
if (template == null) {
75337531
throw new InvalidParameterValueException(String.format("Template for VM [%s] cannot be found.", vm.getUuid()));
75347532
}
75357533

7536-
_accountMgr.checkAccess(newAccount, AccessType.UseEntry, true, template);
7534+
logger.debug("Verifying if new owner [{}] has access to the template [{}].", newAccount, template.getUuid());
7535+
try {
7536+
_accountMgr.checkAccess(newAccount, AccessType.UseEntry, true, template);
7537+
} catch (PermissionDeniedException e) {
7538+
String newMsg = String.format("New owner [%s] does not have access to the template specified for VM [%s].", newAccount, vm);
7539+
throw new PermissionDeniedException(newMsg, e);
7540+
}
75377541
}
75387542

75397543
/**
@@ -7674,6 +7678,8 @@ protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Acc
76747678
protected void validateOldAndNewAccounts(Account oldAccount, Account newAccount, Long oldAccountId, String newAccountName, Long domainId)
76757679
throws InvalidParameterValueException {
76767680

7681+
logger.trace("Validating old [{}] and new accounts [{}].", oldAccount, newAccount);
7682+
76777683
if (oldAccount == null) {
76787684
throw new InvalidParameterValueException(String.format("Invalid old account [%s] for VM in domain [%s].", oldAccountId, domainId));
76797685
}

0 commit comments

Comments
 (0)