Skip to content

Commit e9b20b6

Browse files
committed
Refactor error handling in Agent class
- Moved the handling of InterruptedError to a separate conditional block for improved clarity. - Updated logging to use error level for InterruptedError, enhancing visibility of interruptions. - Simplified the last result state update by removing unnecessary parameters.
1 parent 15ec11e commit e9b20b6

1 file changed

Lines changed: 5 additions & 13 deletions

File tree

browser_use/agent/service.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,6 @@ async def _post_process(self) -> None:
761761
async def _handle_step_error(self, error: Exception) -> None:
762762
"""Handle all types of errors that can occur during a step"""
763763

764-
# Handle InterruptedError specially
765-
if isinstance(error, InterruptedError):
766-
self.logger.debug(f'InterruptedError: {type(error).__name__}: {error}')
767-
self.state.consecutive_failures += 1
768-
769-
self.state.last_result = [
770-
ActionResult(
771-
error='The agent was interrupted mid-step' + (f' - {error}' if error else ''),
772-
)
773-
]
774-
return
775-
776764
# Handle all other exceptions
777765
include_trace = self.logger.isEnabledFor(logging.DEBUG)
778766
error_msg = AgentError.format_error(error, include_trace=include_trace)
@@ -784,6 +772,10 @@ async def _handle_step_error(self, error: Exception) -> None:
784772
if 'Max token limit reached' in error_msg:
785773
# TODO: figure out what to do here
786774
pass
775+
# Handle InterruptedError specially
776+
elif isinstance(error, InterruptedError):
777+
error_msg = 'The agent was interrupted mid-step' + (f' - {error}' if error else '')
778+
self.logger.error(f'{prefix}{error_msg}')
787779
elif 'Could not parse response' in error_msg or 'tool_use_failed' in error_msg:
788780
# give model a hint how output should look like
789781
logger.debug(f'Model: {self.llm.model} failed')
@@ -807,7 +799,7 @@ async def _handle_step_error(self, error: Exception) -> None:
807799
else:
808800
self.logger.error(f'{prefix}{error_msg}')
809801

810-
self.state.last_result = [ActionResult(error=error_msg, include_in_memory=True)]
802+
self.state.last_result = [ActionResult(error=error_msg)]
811803
return None
812804

813805
async def _finalize(self, browser_state_summary: BrowserStateSummary | None) -> None:

0 commit comments

Comments
 (0)