Skip to content

fix: harden agent files API error handling to prevent React crash#1053

Merged
chubes4 merged 1 commit intomainfrom
fix/agent-files-api-error-handling
Apr 14, 2026
Merged

fix: harden agent files API error handling to prevent React crash#1053
chubes4 merged 1 commit intomainfrom
fix/agent-files-api-error-handling

Conversation

@chubes4
Copy link
Copy Markdown
Member

@chubes4 chubes4 commented Apr 14, 2026

Summary

  • Wraps list_agent_files and get_agent_file REST handlers in try/catch (\Throwable) so uncaught exceptions (e.g. missing DB tables during scaffolding) return proper WP_Error responses instead of raw 500s
  • Fixes useAgentFiles query select function to always return an array via Array.isArray() — previously, API error objects ({ success: false, data: null }) passed through the nullish coalescing chain as truthy non-arrays, causing .filter() crashes
  • Hardens .filter() call in handleCreateFile and hasFiles check in AgentApp with Array.isArray() guards

Bug

When loading the Agent admin page, if the /files/agent or /files/agent/SOUL.md endpoints return 500 errors, the React UI crashes with:

TypeError: a.filter is not a function
    at st (agent-react.js:1:45153)

The root cause is a cascading failure:

  1. PHP handlers throw uncaught exceptions → raw 500
  2. API client catches the error and returns { success: false, data: null }
  3. Query select: (response) => response?.data ?? response ?? [] evaluates to the error object (truthy), not []
  4. Component calls .filter() on an object → crash

Testing

  1. Trigger a 500 on /files/agent (e.g. missing agents table)
  2. Navigate to Agent admin page → should show "Failed to load files" instead of crashing
  3. Normal operation with working API → file list loads as before

When the /files/agent REST endpoints return 500 errors (e.g. from
missing database tables during scaffolding), the React query hook's
select function could return a non-array error object instead of an
array. This caused .filter() calls in AgentFileList to crash with
'a.filter is not a function'.

- Wrap list_agent_files and get_agent_file handlers in try/catch
- Fix useAgentFiles select to always return an array via Array.isArray()
- Harden .filter() call in handleCreateFile callback
- Fix hasFiles check in AgentApp to use Array.isArray()
@chubes4 chubes4 merged commit 41c32f5 into main Apr 14, 2026
1 check passed
@chubes4 chubes4 deleted the fix/agent-files-api-error-handling branch April 14, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant