fix(web-fetch): handle network-level fetch errors in fetchFirecrawlContent #4403
+93
−10
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
Fixes #4392 - When
fetch()fails at the network level (timeout, connection refused, DNS failure, etc.), the error was not being caught withinfetchFirecrawlContent, causing unhandled promise rejections that could crash the gateway.Problem
When a sub-agent uses
web_fetchand the underlyingfetch()call fails at the network level (not an HTTP error like 404, but a TCP/connection-level failure), the promise rejection was not caught withinfetchFirecrawlContent. This bubbled up as an unhandled rejection that triggeredprocess.exit(1)in the global handler.Solution
Wrap the
fetch()andres.json()calls infetchFirecrawlContentwith try-catch blocks to:The descriptive errors propagate up to the tool execution wrapper in
pi-tool-definition-adapter.tswhich converts them to proper tool error results, preventing the gateway from crashing.Changes
src/agents/tools/web-fetch.ts- Wrap fetch() and res.json() with try-catch infetchFirecrawlContentsrc/agents/tools/web-tools.fetch.test.ts- Add test cases for network-level errors and invalid JSON responsesTest plan
🤖 Generated with Claude Code