Skip to content

Commit a0a38de

Browse files
cho-thinkfree-comspring-builds
authored andcommitted
Add fallback message for blank tool execution errors
Fixes #4518 Signed-off-by: cho-thinkfree-com <cho@thinkfree.com> (cherry picked from commit f2d57cc)
1 parent 7ff10f5 commit a0a38de

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

spring-ai-model/src/main/java/org/springframework/ai/tool/execution/DefaultToolExecutionExceptionProcessor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ public String process(ToolExecutionException exception) {
7272
if (this.alwaysThrow) {
7373
throw exception;
7474
}
75-
logger.debug("Exception thrown by tool: {}. Message: {}", exception.getToolDefinition().name(),
76-
exception.getMessage());
77-
return exception.getMessage();
75+
String message = exception.getMessage();
76+
if (message == null || message.isBlank()) {
77+
message = "Exception occurred in tool: " + exception.getToolDefinition().name() + " ("
78+
+ cause.getClass().getSimpleName() + ")";
79+
}
80+
logger.debug("Exception thrown by tool: {}. Message: {}", exception.getToolDefinition().name(), message);
81+
return message;
7882
}
7983

8084
public static Builder builder() {

spring-ai-model/src/test/java/org/springframework/ai/tool/execution/DefaultToolExecutionExceptionProcessorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,28 @@ void processReturnsMessage() {
5959
assertThat(result).isEqualTo(this.toolException.getMessage());
6060
}
6161

62+
@Test
63+
void processReturnsFallbackMessageWhenNull() {
64+
DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder().build();
65+
66+
ToolExecutionException exception = new ToolExecutionException(this.toolDefinition, new IllegalStateException());
67+
68+
String result = processor.process(exception);
69+
70+
assertThat(result).isEqualTo("Exception occurred in tool: toolName (IllegalStateException)");
71+
}
72+
73+
@Test
74+
void processReturnsFallbackMessageWhenBlank() {
75+
DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder().build();
76+
77+
ToolExecutionException exception = new ToolExecutionException(this.toolDefinition, new RuntimeException(" "));
78+
79+
String result = processor.process(exception);
80+
81+
assertThat(result).isEqualTo("Exception occurred in tool: toolName (RuntimeException)");
82+
}
83+
6284
@Test
6385
void processAlwaysThrows() {
6486
DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder()

0 commit comments

Comments
 (0)