Claude Code Claude Opus 4.6 commited on
Commit
a5fe23c
·
1 Parent(s): 2be8520

Claude Code: refactor error handling into dedicated error_handlers.py

Browse files

- Create new error_handlers.py with specific exception handlers
- Move try/except blocks from app.py to dedicated handler functions
- handle_status_file_read: handles FileNotFoundError, PermissionError, JSONDecodeError
- handle_brain_response: handles ImportError, AttributeError, KeyError
- handle_websocket_send: handles ConnectionError, RuntimeError
- Refactor app.py to import and use these handlers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +4 -6
  2. error_handlers.py +6 -12
app.py CHANGED
@@ -21,7 +21,7 @@ sys.path.insert(0, "/app")
21
  # Import system logger
22
  from openclaw.core.system_logger import log_startup, log_heartbeat, get_last_lines
23
  # Import error handlers
24
- from error_handlers import handle_status_file_read, handle_brain_response, handle_websocket_message
25
 
26
 
27
  @asynccontextmanager
@@ -149,12 +149,10 @@ async def websocket_endpoint(websocket: WebSocket):
149
  await websocket.accept()
150
  try:
151
  while True:
152
- # Send heartbeat every 5 seconds
153
  status_data = get_cain_status()
154
- await websocket.send_json({
155
- "type": "heartbeat",
156
- "status": status_data
157
- })
158
  await asyncio.sleep(5)
159
  except Exception as e:
160
  pass
 
21
  # Import system logger
22
  from openclaw.core.system_logger import log_startup, log_heartbeat, get_last_lines
23
  # Import error handlers
24
+ from error_handlers import handle_status_file_read, handle_brain_response, handle_websocket_send
25
 
26
 
27
  @asynccontextmanager
 
149
  await websocket.accept()
150
  try:
151
  while True:
152
+ # Send heartbeat every 5 seconds using error handler
153
  status_data = get_cain_status()
154
+ if not await handle_websocket_send(websocket, status_data):
155
+ break
 
 
156
  await asyncio.sleep(5)
157
  except Exception as e:
158
  pass
error_handlers.py CHANGED
@@ -85,28 +85,22 @@ def handle_brain_response(message: str) -> str:
85
  return f"Brain processing error: {type(e).__name__}: {str(e)}"
86
 
87
 
88
- def handle_websocket_message(websocket, status_func, sleep_func) -> bool:
89
  """
90
- Handle websocket message loop with specific exception handling.
91
 
92
  Args:
93
  websocket: The WebSocket connection object.
94
- status_func: Function to get current status.
95
- sleep_func: Async sleep function.
96
 
97
  Returns:
98
- True if loop should continue, False if it should break.
99
  """
100
  try:
101
- status_data = status_func()
102
- websocket.send_json({
103
  "type": "heartbeat",
104
  "status": status_data
105
  })
106
  return True
107
- except ConnectionError:
108
- return False
109
- except RuntimeError:
110
- return False
111
- except Exception:
112
  return False
 
85
  return f"Brain processing error: {type(e).__name__}: {str(e)}"
86
 
87
 
88
+ async def handle_websocket_send(websocket, status_data: dict) -> bool:
89
  """
90
+ Handle websocket send with specific exception handling.
91
 
92
  Args:
93
  websocket: The WebSocket connection object.
94
+ status_data: Status data to send.
 
95
 
96
  Returns:
97
+ True if send succeeded, False if connection should close.
98
  """
99
  try:
100
+ await websocket.send_json({
 
101
  "type": "heartbeat",
102
  "status": status_data
103
  })
104
  return True
105
+ except (ConnectionError, RuntimeError, Exception):
 
 
 
 
106
  return False