Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ai-fanatic
Copy link

Summary

Fixes #4392 - When fetch() fails at the network level (timeout, connection refused, DNS failure, etc.), the error was not being caught within fetchFirecrawlContent, causing unhandled promise rejections that could crash the gateway.

Problem

When a sub-agent uses web_fetch and the underlying fetch() call fails at the network level (not an HTTP error like 404, but a TCP/connection-level failure), the promise rejection was not caught within fetchFirecrawlContent. This bubbled up as an unhandled rejection that triggered process.exit(1) in the global handler.

Solution

Wrap the fetch() and res.json() calls in fetchFirecrawlContent with try-catch blocks to:

  • Catch network-level fetch failures (TypeError: fetch failed) and throw a descriptive error
  • Catch invalid JSON response errors and throw a descriptive error

The descriptive errors propagate up to the tool execution wrapper in pi-tool-definition-adapter.ts which 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 in fetchFirecrawlContent
  • src/agents/tools/web-tools.fetch.test.ts - Add test cases for network-level errors and invalid JSON responses

Test plan

  • Added test case for network-level fetch failure in firecrawl fallback
  • Added test case for invalid JSON response from firecrawl
  • Manual verification: Trigger a network timeout during web_fetch and verify error is returned as tool result instead of crashing

🤖 Generated with Claude Code

…ntent

When fetch() fails at the network level (timeout, connection refused, etc.),
the error was not being caught within fetchFirecrawlContent, causing
unhandled promise rejections that could crash the gateway.

This change wraps the fetch() and res.json() calls in try-catch blocks to:
- Catch network-level fetch failures and throw a descriptive error
- Catch invalid JSON response errors and throw a descriptive error

The descriptive errors propagate up to the tool execution wrapper which
converts them to proper tool error results.

Fixes openclaw#4392

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@moltbot-barnacle moltbot-barnacle bot added the agents Agent runtime and tooling label Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unhandled promise rejection in web_fetch crashes gateway when fetch() fails at network level

1 participant