Annotation Interface ErrorHandler


@Retention(RUNTIME) @Target(METHOD) public @interface ErrorHandler
Marks a method as an error handler for a workflow agent. The annotated method must be static on the agent type, an ErrorContext as argument and return an instance of ErrorRecoveryResult. Besides the ErrorContext, it can declare parameters resolved through a SupplierParameterResolver. It will be invoked when an error occurs during the agent's operation, allowing for custom error handling logic.

Example:


    public interface StoryCreatorWithErrorRecovery {

        @SequenceAgent(outputKey = "story",
                subAgents = { CreativeWriter.class, AudienceEditor.class, StyleEditor.class })
        String write(@V("topic") String topic, @V("style") String style, @V("audience") String audience);

        @ErrorHandler
        static ErrorRecoveryResult errorHandler(ErrorContext errorContext) {
            if (errorContext.agentName().equals("generateStory") &&
                    errorContext.exception() instanceof MissingArgumentException mEx && mEx.argumentName().equals("topic")) {
                errorContext.agenticScope().writeState("topic", "dragons and wizards");
                return ErrorRecoveryResult.retry();
            }
            return ErrorRecoveryResult.throwException();
        }
    }

Like every helper method, it can be an instance method of a helper object of the agent instead of a static method of the agent type: see the "Helper objects" section of the agentic tutorial and AgenticServices.AgentConfigurator.