Skip to content

Commit a541ad5

Browse files
authored
Java: Fix logging config parsing errors. Fixes #3238 (#3321)
Fix #3238
1 parent b871957 commit a541ad5

5 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎java/semantickernel-api/src/main/java/com/microsoft/semantickernel/KernelException.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public enum ErrorCodes {
4646

4747
FUNCTION_TYPE_NOT_SUPPORTED("Function type not supported"),
4848

49+
FUNCTION_CONFIGURATION_ERROR("Function configuration is not valid"),
50+
4951
INVALID_FUNCTION_TYPE("Invalid function type"),
5052

5153
INVALID_SERVICE_CONFIGURATION("Invalid service configuration"),

‎java/semantickernel-api/src/main/java/com/microsoft/semantickernel/SkillExecutor.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ ReadOnlyFunctionCollection importSkillFromDirectory(
6060
* @param skillName name of the skill
6161
* @param functionName name of the function
6262
* @return the function collection
63+
* @throws KernelException if it is not possible to correctly load the skill
6364
*/
6465
ReadOnlyFunctionCollection importSkillFromResources(
65-
String pluginDirectory, String skillName, String functionName);
66+
String pluginDirectory, String skillName, String functionName) throws KernelException;
6667

6768
/**
6869
* Imports a skill using clazz.getResourceAsStream to load skills from the classpath.
@@ -71,9 +72,10 @@ ReadOnlyFunctionCollection importSkillFromResources(
7172
* @param functionName name of the function
7273
* @param clazz class that contains the skill
7374
* @return the function collection
75+
* @throws KernelException if it is not possible to correctly load the skill
7476
*/
7577
ReadOnlyFunctionCollection importSkillFromResources(
76-
String pluginDirectory, String skillName, String functionName, @Nullable Class clazz);
78+
String pluginDirectory, String skillName, String functionName, @Nullable Class clazz) throws KernelException;
7779

7880
/**
7981
* Imports the native functions annotated on the given object as a skill.

‎java/semantickernel-api/src/main/java/com/microsoft/semantickernel/extensions/KernelExtensions.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
package com.microsoft.semantickernel.extensions;
33

4+
import com.fasterxml.jackson.databind.JsonMappingException;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.microsoft.semantickernel.KernelException;
67
import com.microsoft.semantickernel.KernelException.ErrorCodes;
@@ -96,7 +97,7 @@ public static Map<String, SemanticFunctionConfig> importSemanticSkillFromResourc
9697
String pluginName,
9798
String functionName,
9899
@Nullable Class clazz,
99-
PromptTemplateEngine promptTemplateEngine) {
100+
PromptTemplateEngine promptTemplateEngine) throws KernelException {
100101

101102
PromptTemplateConfig config =
102103
getPromptTemplateConfig(pluginDirectory, pluginName, functionName, clazz);
@@ -149,7 +150,7 @@ private static String getTemplatePrompt(
149150
}
150151

151152
private static PromptTemplateConfig getPromptTemplateConfig(
152-
String pluginDirectory, String pluginName, String functionName, @Nullable Class clazz) {
153+
String pluginDirectory, String pluginName, String functionName, @Nullable Class clazz) throws KernelException {
153154
String configFileName =
154155
pluginDirectory
155156
+ File.separator
@@ -168,10 +169,18 @@ private static PromptTemplateConfig getPromptTemplateConfig(
168169
ResourceLocation.CLASSPATH,
169170
ResourceLocation.FILESYSTEM);
170171

171-
return new ObjectMapper().readValue(config, PromptTemplateConfig.class);
172+
return new ObjectMapper().readValue(config, PromptTemplateConfig.class);
172173
} catch (IOException e) {
174+
if (e instanceof JsonMappingException) {
175+
LOGGER.error("Failed to parse config file " + configFileName, e);
176+
177+
throw new KernelException(
178+
ErrorCodes.FUNCTION_CONFIGURATION_ERROR,
179+
"Failed to parse config file " + configFileName, e);
180+
} else {
173181
LOGGER.debug("No config for " + functionName + " in " + pluginName);
174-
return null;
182+
}
183+
return null;
175184
}
176185
}
177186
}

‎java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplateConfig.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.microsoft.semantickernel.semanticfunctions;
33

44
import com.fasterxml.jackson.annotation.JsonCreator;
5+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67
import com.microsoft.semantickernel.builders.Buildable;
78
import com.microsoft.semantickernel.builders.BuildersSingleton;
@@ -12,6 +13,7 @@
1213
import reactor.util.annotation.Nullable;
1314

1415
/** Prompt template configuration */
16+
@JsonIgnoreProperties(ignoreUnknown = true)
1517
public class PromptTemplateConfig {
1618
private final CompletionConfig completionConfig;
1719
private final InputConfig input;

‎java/semantickernel-core/src/main/java/com/microsoft/semantickernel/DefaultKernel.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public ReadOnlyFunctionCollection importSkillFromResources(
235235

236236
@Override
237237
public ReadOnlyFunctionCollection importSkillFromResources(
238-
String pluginDirectory, String skillName, String functionName, @Nullable Class clazz) {
238+
String pluginDirectory, String skillName, String functionName, @Nullable Class clazz) throws KernelException {
239239
Map<String, SemanticFunctionConfig> skills =
240240
KernelExtensions.importSemanticSkillFromResourcesDirectory(
241241
pluginDirectory, skillName, functionName, clazz, promptTemplateEngine);

0 commit comments

Comments
 (0)