1313
1414import dev .langchain4j .agent .tool .ReturnBehavior ;
1515import dev .langchain4j .agent .tool .ToolExecutionRequest ;
16+ import dev .langchain4j .exception .ToolExecutionException ;
1617import dev .langchain4j .internal .Json ;
1718import dev .langchain4j .invocation .InvocationContext ;
1819import dev .langchain4j .service .tool .ToolExecutionResult ;
@@ -29,7 +30,8 @@ public class QuarkusToolExecutor implements ToolExecutor {
2930 private final Context context ;
3031
3132 public record Context (Object tool , String toolInvokerName , String methodName , String argumentMapperClassName ,
32- ToolMethodCreateInfo .ExecutionModel executionModel , ReturnBehavior returnBehavior ) {
33+ ToolMethodCreateInfo .ExecutionModel executionModel , ReturnBehavior returnBehavior ,
34+ boolean propagateToolExecutionExceptions ) {
3335 }
3436
3537 public interface Wrapper {
@@ -107,7 +109,7 @@ private ToolExecutionResult invoke(Object[] params, ToolInvoker invokerInstance)
107109 String result ;
108110 if (invocationResult instanceof Uni <?>) { // TODO CS
109111 if (io .vertx .core .Context .isOnEventLoopThread ()) {
110- throw new IllegalStateException (
112+ throw new ToolExecutionException (
111113 "Cannot execute tools returning Uni on event loop thread due to a tool executor limitation" );
112114 }
113115 result = handleResult (invokerInstance , ((Uni <?>) invocationResult ).await ().indefinitely ());
@@ -116,12 +118,11 @@ private ToolExecutionResult invoke(Object[] params, ToolInvoker invokerInstance)
116118 }
117119 log .debugv ("Tool execution result: {0}" , result );
118120 return ToolExecutionResult .builder ().result (invocationResult ).resultText (result ).build ();
121+ } catch (ToolExecutionException e ) {
122+ throw e ;
119123 } catch (Exception e ) {
120- if (e instanceof IllegalArgumentException ) {
121- throw (IllegalArgumentException ) e ;
122- }
123- if (e instanceof IllegalStateException ) {
124- throw (IllegalStateException ) e ;
124+ if (context .propagateToolExecutionExceptions ) {
125+ throw new ToolExecutionException (e );
125126 }
126127 log .error ("Error while executing tool '" + context .tool .getClass () + "'" , e );
127128 return ToolExecutionResult .builder ().isError (true ).resultText (e .getMessage ()).build ();
0 commit comments