From bb6518f0d7e600ce5ad5343ea89ceddb7081e03f Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 25 Jun 2026 13:53:41 +0530 Subject: [PATCH] refactor Signed-off-by: Abhishek Kumar --- .../api/command/user/vm/DeployVMCmd.java | 6 +- .../context/ResponseMessageResolver.java | 36 ---------- .../apache/cloudstack/error/Exceptions.java | 68 +++++++++++++++++++ 3 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 api/src/main/java/org/apache/cloudstack/error/Exceptions.java diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java index 98845d02075..c4e8a74186f 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java @@ -32,7 +32,7 @@ import org.apache.cloudstack.api.response.TemplateResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.api.response.VolumeResponse; import org.apache.cloudstack.context.CallContext; -import org.apache.cloudstack.context.ResponseMessageResolver; +import org.apache.cloudstack.error.Exceptions; import com.cloud.configuration.Resource; import com.cloud.exception.ConcurrentOperationException; @@ -166,10 +166,10 @@ public class DeployVMCmd extends BaseDeployVMCmd { protected void handleCreateResourceAllocationException(ResourceAllocationException ex) { Object cause = CallContext.current().getErrorContextParameters().get("resourceLimitCause"); if (Resource.ResourceOwnerType.Account.equals(cause)) { - throw ResponseMessageResolver.serverApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, + throw Exceptions.serverApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, "vm.deploy.resourcelimit.exceeded.account"); } else if (Resource.ResourceOwnerType.Domain.equals(cause)) { - throw ResponseMessageResolver.serverApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, + throw Exceptions.serverApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, "vm.deploy.resourcelimit.exceeded.domain"); } throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage()); diff --git a/api/src/main/java/org/apache/cloudstack/context/ResponseMessageResolver.java b/api/src/main/java/org/apache/cloudstack/context/ResponseMessageResolver.java index c4dac6afcf3..d313692a9ff 100644 --- a/api/src/main/java/org/apache/cloudstack/context/ResponseMessageResolver.java +++ b/api/src/main/java/org/apache/cloudstack/context/ResponseMessageResolver.java @@ -30,10 +30,8 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.cloudstack.api.ApiErrorCode; import org.apache.cloudstack.api.Identity; import org.apache.cloudstack.api.InternalIdentity; -import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.response.ExceptionResponse; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.ObjectUtils; @@ -41,8 +39,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.cloud.exception.InvalidParameterValueException; -import com.cloud.exception.PermissionDeniedException; import com.cloud.utils.PropertiesUtil; import com.cloud.utils.Ternary; import com.cloud.utils.exception.CloudRuntimeException; @@ -370,36 +366,4 @@ public class ResponseMessageResolver { response.setErrorText(expand(template, stringMap)); response.setErrorMetadata(stringMap); } - - public static InvalidParameterValueException invalidParameterValueException(String errorKey, Map metadata) { - return new InvalidParameterValueException(errorKey, metadata); - } - - public static InvalidParameterValueException invalidParameterValueException(String errorKey) { - return invalidParameterValueException(errorKey, Collections.emptyMap()); - } - - public static PermissionDeniedException permissionDeniedException(String errorKey, Map metadata) { - return new PermissionDeniedException(errorKey, metadata); - } - - public static PermissionDeniedException permissionDeniedException(String errorKey) { - return permissionDeniedException(errorKey, Collections.emptyMap()); - } - - public static ServerApiException serverApiException(ApiErrorCode errorCode, String errorKey) { - return new ServerApiException(errorCode, errorKey, Collections.emptyMap()); - } - - public static CloudRuntimeException cloudRuntimeException(String errorKey, Map metadata) { - return new CloudRuntimeException(resolve(errorKey, metadata)); - } - - public static CloudRuntimeException cloudRuntimeException(String errorKey) { - return new CloudRuntimeException(resolve(errorKey, Collections.emptyMap())); - } - - public static CloudRuntimeException cloudRuntimeException(String errorKey, Map metadata, Throwable th) { - return new CloudRuntimeException(resolve(errorKey, metadata), th); - } } diff --git a/api/src/main/java/org/apache/cloudstack/error/Exceptions.java b/api/src/main/java/org/apache/cloudstack/error/Exceptions.java new file mode 100644 index 00000000000..544d6468f47 --- /dev/null +++ b/api/src/main/java/org/apache/cloudstack/error/Exceptions.java @@ -0,0 +1,68 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.error; + +import java.util.Collections; +import java.util.Map; + +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.context.ResponseMessageResolver; + +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.exception.PermissionDeniedException; +import com.cloud.utils.exception.CloudRuntimeException; + +public class Exceptions { + + public static InvalidParameterValueException invalidParameterValueException(String errorKey, Map metadata) { + return new InvalidParameterValueException(errorKey, metadata); + } + + public static InvalidParameterValueException invalidParameterValueException(String errorKey) { + return invalidParameterValueException(errorKey, Collections.emptyMap()); + } + + public static PermissionDeniedException permissionDeniedException(String errorKey, Map metadata) { + return new PermissionDeniedException(errorKey, metadata); + } + + public static PermissionDeniedException permissionDeniedException(String errorKey) { + return permissionDeniedException(errorKey, Collections.emptyMap()); + } + + public static ServerApiException serverApiException(ApiErrorCode errorCode, String errorKey, Map metadata) { + return new ServerApiException(errorCode, errorKey, metadata); + } + + public static ServerApiException serverApiException(ApiErrorCode errorCode, String errorKey) { + return serverApiException(errorCode, errorKey, Collections.emptyMap()); + } + + public static CloudRuntimeException cloudRuntimeException(String errorKey, Map metadata) { + return new CloudRuntimeException(ResponseMessageResolver.resolve(errorKey, metadata)); + } + + public static CloudRuntimeException cloudRuntimeException(String errorKey) { + return new CloudRuntimeException(ResponseMessageResolver.resolve(errorKey, Collections.emptyMap())); + } + + public static CloudRuntimeException cloudRuntimeException(String errorKey, Map metadata, Throwable th) { + return new CloudRuntimeException(ResponseMessageResolver.resolve(errorKey, metadata), th); + } +}