Prompt
Implement error handling with clear intent and proper propagation. Follow these principles:
- Use structured try/catch blocks with specific error types
- Propagate errors explicitly - avoid silent failures
- Include actionable error messages
- Consider creating custom exceptions for domain-specific errors
Example of proper error handling structure:
try:
if self.max_execution_time is not None:
if not isinstance(self.max_execution_time, int) or self.max_execution_time <= 0:
raise ValueError("Max Execution time must be a positive integer greater than zero")
result = self._execute_with_timeout(task_prompt, task, self.max_execution_time)
else:
result = self._execute_without_timeout(task_prompt, task)
except TimeoutError as e:
error = f"Task '{task.description}' execution timed out after {self.max_execution_time} seconds. Consider increasing max_execution_time or optimizing the task."
crewai_event_bus.emit(
self,
event=AgentExecutionErrorEvent(
agent=self,
task=task,
error=error
)
)
raise TimeoutError(error)
This pattern ensures:
- Specific error types are caught and handled appropriately
- Error messages are clear and actionable
- Errors are properly propagated up the call stack
- Error state is consistently communicated through events/logging