mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 20:02:29 +01:00
Throw server api exception with correct error code
This commit is contained in:
parent
dd9c60e341
commit
996d0edbc9
@ -65,8 +65,7 @@ public class ApiDispatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void dispatchCreateCmd(BaseAsyncCreateCmd cmd, Map<String, String> params) {
|
public void dispatchCreateCmd(BaseAsyncCreateCmd cmd, Map<String, String> params) {
|
||||||
|
|
||||||
String errorMsg = "";
|
|
||||||
setupParameters(cmd, params);
|
setupParameters(cmd, params);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -74,109 +73,84 @@ public class ApiDispatcher {
|
|||||||
ctx.setAccountId(cmd.getEntityOwnerId());
|
ctx.setAccountId(cmd.getEntityOwnerId());
|
||||||
cmd.create();
|
cmd.create();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
|
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Parameter error";
|
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
|
||||||
} else if (t instanceof PermissionDeniedException) {
|
} else if (t instanceof PermissionDeniedException) {
|
||||||
s_logger.info("PermissionDenied: " + t.getMessage());
|
s_logger.info("PermissionDenied: " + t.getMessage());
|
||||||
errorMsg = "Permission denied";
|
|
||||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
|
||||||
} else if (t instanceof AccountLimitException) {
|
} else if (t instanceof AccountLimitException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Account resource limit error";
|
|
||||||
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
|
||||||
}else if (t instanceof InsufficientCapacityException) {
|
} else if (t instanceof InsufficientCapacityException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Insufficient capacity";
|
|
||||||
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
|
||||||
} else if (t instanceof ResourceAllocationException) {
|
} else if (t instanceof ResourceAllocationException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Resource allocation error";
|
|
||||||
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
|
||||||
} else if (t instanceof ResourceUnavailableException) {
|
} else if (t instanceof ResourceUnavailableException) {
|
||||||
s_logger.warn("Exception: ", t);
|
s_logger.warn("Exception: ", t);
|
||||||
errorMsg = "Resource unavailable error";
|
|
||||||
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
|
||||||
} else if (t instanceof AsyncCommandQueued) {
|
} else if (t instanceof AsyncCommandQueued) {
|
||||||
throw (AsyncCommandQueued)t;
|
throw (AsyncCommandQueued) t;
|
||||||
} else if (t instanceof ServerApiException) {
|
} else if (t instanceof ServerApiException) {
|
||||||
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
|
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
|
||||||
errorMsg = ((ServerApiException) t).getDescription();
|
throw (ServerApiException) t;
|
||||||
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg.length() > 0 ? errorMsg : ((ServerApiException) t).getDescription());
|
|
||||||
} else {
|
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
errorMsg = "Internal error";
|
|
||||||
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
|
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
|
||||||
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
|
||||||
} else {
|
} else {
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
|
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispatch(BaseCmd cmd, Map<String, String> params) {
|
public void dispatch(BaseCmd cmd, Map<String, String> params) {
|
||||||
String errorMsg = "";
|
|
||||||
setupParameters(cmd, params);
|
setupParameters(cmd, params);
|
||||||
try {
|
try {
|
||||||
UserContext ctx = UserContext.current();
|
UserContext ctx = UserContext.current();
|
||||||
ctx.setAccountId(cmd.getEntityOwnerId());
|
ctx.setAccountId(cmd.getEntityOwnerId());
|
||||||
if(cmd instanceof BaseAsyncCmd){
|
if (cmd instanceof BaseAsyncCmd) {
|
||||||
|
|
||||||
BaseAsyncCmd asyncCmd = (BaseAsyncCmd)cmd;
|
BaseAsyncCmd asyncCmd = (BaseAsyncCmd) cmd;
|
||||||
String startEventId = params.get("ctxStartEventId");
|
String startEventId = params.get("ctxStartEventId");
|
||||||
ctx.setStartEventId(Long.valueOf(startEventId));
|
ctx.setStartEventId(Long.valueOf(startEventId));
|
||||||
|
|
||||||
//Synchronise job on the object if needed
|
// Synchronise job on the object if needed
|
||||||
if (asyncCmd.getJob() != null && asyncCmd.getSyncObjId() != null && asyncCmd.getSyncObjType() != null) {
|
if (asyncCmd.getJob() != null && asyncCmd.getSyncObjId() != null && asyncCmd.getSyncObjType() != null) {
|
||||||
_asyncMgr.syncAsyncJobExecution(asyncCmd.getJob(), asyncCmd.getSyncObjType(), asyncCmd.getSyncObjId().longValue());
|
_asyncMgr.syncAsyncJobExecution(asyncCmd.getJob(), asyncCmd.getSyncObjType(), asyncCmd.getSyncObjId().longValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.execute();
|
cmd.execute();
|
||||||
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
|
if (t instanceof InvalidParameterValueException || t instanceof IllegalArgumentException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Parameter error";
|
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, t.getMessage());
|
||||||
} else if (t instanceof PermissionDeniedException) {
|
} else if (t instanceof PermissionDeniedException) {
|
||||||
s_logger.info("PermissionDenied: " + t.getMessage());
|
s_logger.info("PermissionDenied: " + t.getMessage());
|
||||||
errorMsg = "Permission denied";
|
|
||||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, t.getMessage());
|
||||||
} else if (t instanceof AccountLimitException) {
|
} else if (t instanceof AccountLimitException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Account resource limit error";
|
|
||||||
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.ACCOUNT_RESOURCE_LIMIT_ERROR, t.getMessage());
|
||||||
} else if (t instanceof InsufficientCapacityException) {
|
} else if (t instanceof InsufficientCapacityException) {
|
||||||
s_logger.info(t.getMessage());
|
s_logger.info(t.getMessage());
|
||||||
errorMsg = "Insufficient capacity";
|
|
||||||
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, t.getMessage());
|
||||||
} else if (t instanceof ResourceAllocationException) {
|
} else if (t instanceof ResourceAllocationException) {
|
||||||
s_logger.warn("Exception: ", t);
|
s_logger.warn("Exception: ", t);
|
||||||
errorMsg = "Resource allocation error";
|
|
||||||
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.RESOURCE_ALLOCATION_ERROR, t.getMessage());
|
||||||
} else if (t instanceof ResourceUnavailableException) {
|
} else if (t instanceof ResourceUnavailableException) {
|
||||||
s_logger.warn("Exception: ", t);
|
s_logger.warn("Exception: ", t);
|
||||||
errorMsg = "Resource unavailable error";
|
|
||||||
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, t.getMessage());
|
||||||
} else if (t instanceof AsyncCommandQueued) {
|
} else if (t instanceof AsyncCommandQueued) {
|
||||||
throw (AsyncCommandQueued)t;
|
throw (AsyncCommandQueued) t;
|
||||||
} else if (t instanceof ServerApiException) {
|
} else if (t instanceof ServerApiException) {
|
||||||
errorMsg = ((ServerApiException) t).getDescription();
|
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
|
||||||
s_logger.warn(t.getClass() + " : " + ((ServerApiException) t).getDescription());
|
throw (ServerApiException) t;
|
||||||
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, errorMsg);
|
|
||||||
} else {
|
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, BaseCmd.USER_ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
errorMsg = "Internal error";
|
|
||||||
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
|
s_logger.error("Exception while executing " + cmd.getClass().getSimpleName() + ":", t);
|
||||||
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
if (UserContext.current().getCaller().getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
||||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
|
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, t.getMessage());
|
||||||
@ -187,17 +161,17 @@ public class ApiDispatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setupParameters(BaseCmd cmd, Map<String, String> params){
|
public static void setupParameters(BaseCmd cmd, Map<String, String> params) {
|
||||||
Map<String, Object> unpackedParams = cmd.unpackParams(params);
|
Map<String, Object> unpackedParams = cmd.unpackParams(params);
|
||||||
|
|
||||||
if (cmd instanceof BaseListCmd) {
|
if (cmd instanceof BaseListCmd) {
|
||||||
if ((unpackedParams.get(ApiConstants.PAGE) == null) && (unpackedParams.get(ApiConstants.PAGE_SIZE) != null)) {
|
if ((unpackedParams.get(ApiConstants.PAGE) == null) && (unpackedParams.get(ApiConstants.PAGE_SIZE) != null)) {
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "\"page\" parameter is required when \"pagesize\" is specified");
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, "\"page\" parameter is required when \"pagesize\" is specified");
|
||||||
} else if ((unpackedParams.get(ApiConstants.PAGE_SIZE) == null) && (unpackedParams.get(ApiConstants.PAGE) != null)) {
|
} else if ((unpackedParams.get(ApiConstants.PAGE_SIZE) == null) && (unpackedParams.get(ApiConstants.PAGE) != null)) {
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "\"pagesize\" parameter is required when \"page\" is specified");
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, "\"pagesize\" parameter is required when \"page\" is specified");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Field[] fields = cmd.getClass().getDeclaredFields();
|
Field[] fields = cmd.getClass().getDeclaredFields();
|
||||||
Class<?> superClass = cmd.getClass().getSuperclass();
|
Class<?> superClass = cmd.getClass().getSuperclass();
|
||||||
while (BaseCmd.class.isAssignableFrom(superClass)) {
|
while (BaseCmd.class.isAssignableFrom(superClass)) {
|
||||||
@ -232,7 +206,8 @@ public class ApiDispatcher {
|
|||||||
if (s_logger.isDebugEnabled()) {
|
if (s_logger.isDebugEnabled()) {
|
||||||
s_logger.debug("Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name());
|
s_logger.debug("Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name());
|
||||||
}
|
}
|
||||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name());
|
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter "
|
||||||
|
+ parameterAnnotation.name());
|
||||||
} catch (ParseException parseEx) {
|
} catch (ParseException parseEx) {
|
||||||
if (s_logger.isDebugEnabled()) {
|
if (s_logger.isDebugEnabled()) {
|
||||||
s_logger.debug("Invalid date parameter " + paramObj + " passed to command " + cmd.getCommandName());
|
s_logger.debug("Invalid date parameter " + paramObj + " passed to command " + cmd.getCommandName());
|
||||||
|
|||||||
@ -687,9 +687,9 @@ public class Upgrade218to22 implements DbUpgrade {
|
|||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
|
||||||
s_logger.debug("Marking " + allocatedIps.size() + " ip addresses to belong to network " + networkId);
|
s_logger.debug("Marking " + allocatedIps.size() + " ip addresses to belong to network " + networkId);
|
||||||
|
s_logger.debug("Updating mac addresses for data center id=" + dcId + ". Found " + allocatedIps.size() + " ip addresses to update");
|
||||||
|
|
||||||
for (Object[] allocatedIp : allocatedIps) {
|
for (Object[] allocatedIp : allocatedIps) {
|
||||||
s_logger.debug("Updating mac addresses for data center id=" + dcId);
|
|
||||||
pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");
|
pstmt = conn.prepareStatement("SELECT mac_address FROM data_center WHERE id = ?");
|
||||||
pstmt.setLong(1, dcId);
|
pstmt.setLong(1, dcId);
|
||||||
rs = pstmt.executeQuery();
|
rs = pstmt.executeQuery();
|
||||||
@ -908,10 +908,10 @@ public class Upgrade218to22 implements DbUpgrade {
|
|||||||
pstmt.setLong(2, vlanId);
|
pstmt.setLong(2, vlanId);
|
||||||
pstmt.executeUpdate();
|
pstmt.executeUpdate();
|
||||||
pstmt.close();
|
pstmt.close();
|
||||||
|
|
||||||
upgradeDirectUserIpAddress(conn, dcId, basicDefaultDirectNetworkId, "DirectAttached");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
upgradeDirectUserIpAddress(conn, dcId, basicDefaultDirectNetworkId, "DirectAttached");
|
||||||
|
|
||||||
// update Dhcp servers information in domain_router and vm_instance tables; all domRs belong to the same
|
// update Dhcp servers information in domain_router and vm_instance tables; all domRs belong to the same
|
||||||
// network
|
// network
|
||||||
pstmt = conn
|
pstmt = conn
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user