api: Throw InvalidParameterValueException for failing ApiArgValidator (#3108)

This is important because it helps in communicating back the exact
error to the API callee.

Current behavior is that ParamProcessWorker#processParameters catches
the exception and returns an incorrect type exception without the
proper message.
This commit is contained in:
Anurag Awasthi 2018-12-24 23:50:55 +05:30 committed by Rohit Yadav
parent 2699586d92
commit 3279707030
1 changed files with 6 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import static org.apache.commons.lang.StringUtils.isNotBlank;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -30,18 +31,15 @@ import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.text.SimpleDateFormat;
import javax.inject.Inject;
import com.google.common.base.Strings;
import org.apache.log4j.Logger;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.acl.InfrastructureEntity;
import org.apache.cloudstack.acl.SecurityChecker;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseAsyncCreateCmd;
import org.apache.cloudstack.api.BaseCmd;
@ -50,7 +48,6 @@ import org.apache.cloudstack.api.EntityReference;
import org.apache.cloudstack.api.InternalIdentity;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.command.admin.resource.ArchiveAlertsCmd;
import org.apache.cloudstack.api.command.admin.resource.DeleteAlertsCmd;
import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
@ -58,6 +55,7 @@ import org.apache.cloudstack.api.command.user.event.ArchiveEventsCmd;
import org.apache.cloudstack.api.command.user.event.DeleteEventsCmd;
import org.apache.cloudstack.api.command.user.event.ListEventsCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.user.Account;
@ -65,6 +63,7 @@ import com.cloud.user.AccountManager;
import com.cloud.utils.DateUtil;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.exception.CloudRuntimeException;
import com.google.common.base.Strings;
public class ParamProcessWorker implements DispatchWorker {
@ -96,7 +95,7 @@ public class ParamProcessWorker implements DispatchWorker {
private void validateNonEmptyString(final Object param, final String argName) {
if (param == null || Strings.isNullOrEmpty(param.toString())) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Empty or null value provided for API arg: %s", argName));
throw new InvalidParameterValueException(String.format("Empty or null value provided for API arg: %s", argName));
}
}
@ -108,7 +107,7 @@ public class ParamProcessWorker implements DispatchWorker {
value = Long.valueOf(param.toString());
}
if (value == null || value < 1L) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, String.format("Invalid value provided for API arg: %s", argName));
throw new InvalidParameterValueException(String.format("Invalid value provided for API arg: %s", argName));
}
}