use separate errorcontext

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2026-01-06 18:13:52 +05:30
parent a9cdfc8133
commit 2e51647e8f
5 changed files with 28 additions and 18 deletions

View File

@ -20,10 +20,10 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.managed.threadlocal.ManagedThreadLocal;
import org.apache.commons.collections.MapUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
@ -68,6 +68,7 @@ public class CallContext {
private User user;
private long userId;
private final Map<Object, Object> context = new HashMap<Object, Object>();
private final Map<String, Object> errorContext = new HashMap<String, Object>();
private Project project;
private String apiName;
@ -418,12 +419,6 @@ public class CallContext {
return context;
}
public Map<String, Object> getContextStringKeyParameters() {
return context.entrySet().stream()
.filter(entry -> entry.getKey() instanceof String)
.collect(Collectors.toMap(entry -> (String) entry.getKey(), Map.Entry::getValue));
}
public void putContextParameters(Map<Object, Object> details){
if (details == null) return;
for(Map.Entry<Object,Object>entry : details.entrySet()){
@ -431,6 +426,21 @@ public class CallContext {
}
}
public Map<String, Object> getErrorContextParameters() {
return errorContext;
}
public void putErrorContextParameter(String key, Object value) {
errorContext.put(key, value);
}
public void putErrorContextParameters(Map<String, Object> details) {
if (MapUtils.isEmpty(details)) {
return;
}
errorContext.putAll(details);
}
public static void setActionEventInfo(String eventType, String description) {
CallContext context = CallContext.current();
if (context != null) {

View File

@ -85,7 +85,7 @@ public class ErrorMessageResolver {
if (variableNames.isEmpty()) {
return metadata;
}
Map<String, Object> contextMetadata = CallContext.current().getContextStringKeyParameters();
Map<String, Object> contextMetadata = CallContext.current().getErrorContextParameters();
if (MapUtils.isEmpty(contextMetadata)) {
return metadata;
}

View File

@ -135,7 +135,7 @@ public class ErrorMessageResolverTest {
public void getCombinedMetadataFromErrorTemplate_shouldReturnEmptyMapWhenContextMetadataIsEmpty() {
String template = "This template has {{var1}}.";
Map<String, Object> metadata = Map.of();
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of());
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of());
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
Assert.assertTrue("Result should be an empty map", result.isEmpty());
}
@ -144,7 +144,7 @@ public class ErrorMessageResolverTest {
public void getCombinedMetadataFromErrorTemplate_shouldCombineContextAndProvidedMetadata() {
String template = "This template has {{var1}} and {{var2}}.";
Map<String, Object> metadata = Map.of("key1", "value1");
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("var1", "valueVar1", "var2", "valueVar2"));
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("var1", "valueVar1", "var2", "valueVar2"));
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
Assert.assertEquals("Result should contain combined metadata", 3, result.size());
Assert.assertEquals("Result should contain context metadata for var1", "valueVar1", result.get("var1"));
@ -156,7 +156,7 @@ public class ErrorMessageResolverTest {
public void getCombinedMetadataFromErrorTemplate_shouldIgnoreVariablesNotInContextMetadata() {
String template = "This template has {{var1}} and {{var2}}.";
Map<String, Object> metadata = Map.of("key1", "value1");
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("var1", "valueVar1"));
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("var1", "valueVar1"));
Map<String, Object> result = ErrorMessageResolver.getCombinedMetadataFromErrorTemplate(template, metadata);
Assert.assertEquals("Result should contain combined metadata", 2, result.size());
Assert.assertEquals("Result should contain context metadata for var1", "valueVar1", result.get("var1"));
@ -332,7 +332,7 @@ public class ErrorMessageResolverTest {
String template = "Error occurred: {{field}}";
Map<String, Object> metadata = Map.of("field", "value");
createTempFileWithTemplate(errorKey, template);
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of());
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of());
String result = ErrorMessageResolver.getMessage(errorKey, metadata);
Assert.assertEquals("Error occurred: value", result);
}
@ -361,7 +361,7 @@ public class ErrorMessageResolverTest {
String template = "Error in {{field1}} and {{field2}}.";
Map<String, Object> metadata = Map.of("field1", "value1");
createTempFileWithTemplate(errorKey, template);
when(callContextMock.getContextStringKeyParameters()).thenReturn(Map.of("field2", "value2"));
when(callContextMock.getErrorContextParameters()).thenReturn(Map.of("field2", "value2"));
String result = ErrorMessageResolver.getMessage(errorKey, metadata);
Assert.assertEquals("Error in value1 and value2.", result);
}

View File

@ -558,7 +558,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
if (domainResourceLimit != Resource.RESOURCE_UNLIMITED && requestedDomainResourceCount > domainResourceLimit) {
String message = "Maximum" + messageSuffix;
Map<Object, Object> details = new HashMap<>();
Map<String, Object> details = new HashMap<>();
details.put("resourceTypeDisplay", StringUtils.isBlank(tag) ? type.getDisplayName() : type.getDisplayName() + " (tag: " + tag + ")");
details.put("resourceOwner", ObjectUtils.firstNonNull(project, account));
details.put("resourceOwnerDomain", domain);
@ -567,7 +567,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
details.put("resourceAmount", convCurrentDomainResourceCount);
details.put("resourceReserved", convCurrentResourceReservation);
details.put("resourceRequested", convNumResources);
CallContext.current().putContextParameters(details);
CallContext.current().putErrorContextParameters(details);
ResourceAllocationException e = new ResourceAllocationException(message, type);
logger.error(message, e);
throw e;
@ -607,7 +607,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
if (accountResourceLimit != Resource.RESOURCE_UNLIMITED && requestedResourceCount > accountResourceLimit) {
String message = "Maximum" + messageSuffix;
Map<Object, Object> details = new HashMap<>();
Map<String, Object> details = new HashMap<>();
details.put("resourceTypeDisplay", StringUtils.isBlank(tag) ? type.getDisplayName() : type.getDisplayName() + " (tag: " + tag + ")");
details.put("resourceOwner", ObjectUtils.firstNonNull(project, account));
details.put("resourceOwnerType", project == null ? "Account" : "Project");
@ -615,7 +615,7 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
details.put("resourceAmount", convertedCurrentResourceCount);
details.put("resourceReserved", convertedCurrentResourceReservation);
details.put("resourceRequested", convertedNumResources);
CallContext.current().putContextParameters(details);
CallContext.current().putErrorContextParameters(details);
ResourceAllocationException e = new ResourceAllocationException(message, type);
logger.error(message, e);
throw e;

View File

@ -6322,7 +6322,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
private void verifyServiceOffering(BaseDeployVMCmd cmd, ServiceOffering serviceOffering) {
CallContext.current().putContextParameter("serviceOffering", serviceOffering);
CallContext.current().putErrorContextParameter("serviceOffering", serviceOffering);
if (ServiceOffering.State.Inactive.equals(serviceOffering.getState())) {
throw new InvalidParameterValueException("vm.deploy.serviceoffering.inactive",
Collections.emptyMap());