fix: harden agent files API error handling to prevent React crash#1053
Merged
fix: harden agent files API error handling to prevent React crash#1053
Conversation
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
list_agent_filesandget_agent_fileREST handlers intry/catch (\Throwable)so uncaught exceptions (e.g. missing DB tables during scaffolding) return properWP_Errorresponses instead of raw 500suseAgentFilesqueryselectfunction to always return an array viaArray.isArray()— previously, API error objects ({ success: false, data: null }) passed through the nullish coalescing chain as truthy non-arrays, causing.filter()crashes.filter()call inhandleCreateFileandhasFilescheck inAgentAppwithArray.isArray()guardsBug
When loading the Agent admin page, if the
/files/agentor/files/agent/SOUL.mdendpoints return 500 errors, the React UI crashes with:The root cause is a cascading failure:
{ success: false, data: null }select: (response) => response?.data ?? response ?? []evaluates to the error object (truthy), not[].filter()on an object → crashTesting
/files/agent(e.g. missing agents table)