Skip to content

Commit

Permalink
update error message with string format in text chunking processor
Browse files Browse the repository at this point in the history
Signed-off-by: yuye-aws <[email protected]>
  • Loading branch information
yuye-aws committed Mar 12, 2024
1 parent faae633 commit dc2c163
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@ private void validateAndParseAlgorithmMap(Map<String, Object> algorithmMap) {
Set<String> supportedChunkers = ChunkerFactory.getAllChunkers();
if (!supportedChunkers.contains(algorithmKey)) {
throw new IllegalArgumentException(
"Unable to create the processor as chunker algorithm ["
+ algorithmKey
+ "] is not supported. Supported chunkers types are "
+ supportedChunkers
String.format(
"Unable to create the processor as chunker algorithm [%s] is not supported. Supported chunkers types are [%s]",
algorithmKey,
supportedChunkers
)
);
}
if (!(algorithmValue instanceof Map)) {
throw new IllegalArgumentException(
"Unable to create the processor as [" + algorithmKey + "] parameters cannot be cast to [" + Map.class.getName() + "]"
String.format("Unable to create the processor as [%s] parameters cannot be cast to [%s]", algorithmKey, Map.class.getName())
);
}
Map<String, Object> chunkerParameters = (Map<String, Object>) algorithmValue;
Expand All @@ -121,12 +122,12 @@ private void validateAndParseAlgorithmMap(Map<String, Object> algorithmMap) {
String maxChunkLimitString = chunkerParameters.get(MAX_CHUNK_LIMIT_FIELD).toString();
if (!(NumberUtils.isParsable(maxChunkLimitString))) {
throw new IllegalArgumentException(
"Parameter [" + MAX_CHUNK_LIMIT_FIELD + "] cannot be cast to [" + Number.class.getName() + "]"
String.format("Parameter [%s] cannot be cast to [%s]", MAX_CHUNK_LIMIT_FIELD, Number.class.getName())
);
}
int maxChunkLimit = NumberUtils.createInteger(maxChunkLimitString);
if (maxChunkLimit <= 0 && maxChunkLimit != DEFAULT_MAX_CHUNK_LIMIT) {
throw new IllegalArgumentException("Parameter [" + MAX_CHUNK_LIMIT_FIELD + "] must be a positive integer");
throw new IllegalArgumentException(String.format("Parameter [%s] must be a positive integer", MAX_CHUNK_LIMIT_FIELD));
}
this.maxChunkLimit = maxChunkLimit;
} else {
Expand Down Expand Up @@ -154,11 +155,11 @@ private int chunkString(String content, List<String> result, Map<String, Object>
chunkCount += contentResult.size();
if (maxChunkLimit != DEFAULT_MAX_CHUNK_LIMIT && chunkCount > maxChunkLimit) {
throw new IllegalArgumentException(
"Unable to create the processor as the number of chunks ["
+ chunkCount
+ "] exceeds the maximum chunk limit ["
+ maxChunkLimit
+ "]"
String.format(
"Unable to create the processor as the number of chunks [%s] exceeds the maximum chunk limit [%s]",
chunkCount,
maxChunkLimit
)
);
}
result.addAll(contentResult);
Expand Down Expand Up @@ -226,7 +227,9 @@ private void validateFieldsValue(IngestDocument ingestDocument) {
if (sourceValue instanceof List || sourceValue instanceof Map) {
validateNestedTypeValue(sourceKey, sourceValue, 1);
} else if (!(sourceValue instanceof String)) {
throw new IllegalArgumentException("field [" + sourceKey + "] is neither string nor nested type, cannot process it");
throw new IllegalArgumentException(
String.format("field [%s] is neither string nor nested type, cannot process it", sourceKey)
);
}
}
}
Expand All @@ -235,7 +238,7 @@ private void validateFieldsValue(IngestDocument ingestDocument) {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void validateNestedTypeValue(String sourceKey, Object sourceValue, int maxDepth) {
if (maxDepth > MapperService.INDEX_MAPPING_DEPTH_LIMIT_SETTING.get(environment.settings())) {
throw new IllegalArgumentException("map type field [" + sourceKey + "] reached max depth limit, cannot process it");
throw new IllegalArgumentException(String.format("map type field [%s] reached max depth limit, cannot process it", sourceKey));
} else if (sourceValue instanceof List) {
validateListTypeValue(sourceKey, sourceValue, maxDepth);
} else if (sourceValue instanceof Map) {
Expand All @@ -244,7 +247,7 @@ private void validateNestedTypeValue(String sourceKey, Object sourceValue, int m
.filter(Objects::nonNull)
.forEach(x -> validateNestedTypeValue(sourceKey, x, maxDepth + 1));
} else if (!(sourceValue instanceof String)) {
throw new IllegalArgumentException("map type field [" + sourceKey + "] has non-string type, cannot process it");
throw new IllegalArgumentException(String.format("map type field [%s] has non-string type, cannot process it", sourceKey));
}
}

Expand All @@ -254,9 +257,11 @@ private void validateListTypeValue(String sourceKey, Object sourceValue, int max
if (value instanceof Map) {
validateNestedTypeValue(sourceKey, value, maxDepth + 1);
} else if (value == null) {
throw new IllegalArgumentException("list type field [" + sourceKey + "] has null, cannot process it");
throw new IllegalArgumentException(String.format("list type field [%s] has null, cannot process it", sourceKey));
} else if (!(value instanceof String)) {
throw new IllegalArgumentException("list type field [" + sourceKey + "] has non string value, cannot process it");
throw new IllegalArgumentException(
String.format("list type field [%s] has non-string value, cannot process it", sourceKey)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void testExecute_withFixedTokenLength_andSourceDataListHybridType_thenFai
() -> processor.execute(ingestDocument)
);
assertEquals(
"list type field [" + INPUT_FIELD + "] has non string value, cannot process it",
"list type field [" + INPUT_FIELD + "] has non-string value, cannot process it",
illegalArgumentException.getMessage()
);
}
Expand Down

0 comments on commit dc2c163

Please sign in to comment.