Skip to content

Comments

fix: handle fetch/API errors in telegram delivery to prevent gateway crashes#2492

Merged
thewilloftheshadow merged 2 commits intoopenclaw:mainfrom
altryne:fix/unhandled-fetch-rejections
Jan 27, 2026
Merged

fix: handle fetch/API errors in telegram delivery to prevent gateway crashes#2492
thewilloftheshadow merged 2 commits intoopenclaw:mainfrom
altryne:fix/unhandled-fetch-rejections

Conversation

@altryne
Copy link

@altryne altryne commented Jan 27, 2026

Summary

  • Wrap all bot.api.sendXxx() media calls in delivery.ts with error handler that logs failures before re-throwing
  • Wrap fetch() in telegram onboarding with try/catch to gracefully handle network errors

Problem

The gateway crashes due to unhandled promise rejections from Telegram API calls. When bot.api.sendPhoto() (or other media sends) fails due to network issues, the error propagates as an unhandled rejection and triggers process.exit(1) in the global handler.

Before: Network failure → unhandled rejection → gateway crash
After: Network failure → logged with context → error propagates to caller → graceful handling

Changes

src/telegram/bot/delivery.ts

Added withMediaErrorHandler() wrapper that:

  1. Catches errors from media API calls
  2. Logs them with context via runtime.error()
  3. Re-throws so callers can handle appropriately
await withMediaErrorHandler("sendPhoto", runtime, () =>
  bot.api.sendPhoto(chatId, file, { ...mediaParams }),
);

src/channels/plugins/onboarding/telegram.ts

Wrapped the fetch() call for username lookup:

  • Returns null on network errors (user can retry with numeric ID)
  • Also checks res.ok before parsing JSON

Test Plan

  • Verify gateway doesn't crash when Telegram API times out
  • Verify media sends still work normally
  • Verify onboarding handles network errors gracefully

Fixes #2487
Fixes #2272

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle bot added the channel: telegram Channel integration: telegram label Jan 27, 2026
@thewilloftheshadow thewilloftheshadow self-assigned this Jan 27, 2026
wolfred and others added 2 commits January 26, 2026 20:03
…crashes

Wrap all bot.api.sendXxx() media calls in delivery.ts with error handler
that logs failures before re-throwing. This ensures network failures are
properly logged with context instead of causing unhandled promise rejections
that crash the gateway.

Also wrap the fetch() call in telegram onboarding with try/catch to
gracefully handle network errors during username lookup.

Fixes openclaw#2487

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@thewilloftheshadow thewilloftheshadow force-pushed the fix/unhandled-fetch-rejections branch from d0d70ee to b3bb956 Compare January 27, 2026 02:03
@thewilloftheshadow thewilloftheshadow merged commit 5796a92 into openclaw:main Jan 27, 2026
17 of 23 checks passed
@thewilloftheshadow
Copy link
Member

Landed via temp rebase onto main.

  • Related tests: pnpm vitest run --config vitest.unit.config.ts src/telegram/bot/delivery.test.ts
  • Land commit: b3bb956
  • Merge commit: 5796a92

Thanks @altryne!

@thewilloftheshadow
Copy link
Member

Landed follow-up changelog/README updates after the PR was already merged.

  • Related tests: pnpm vitest run --config vitest.unit.config.ts src/telegram/bot/delivery.test.ts src/telegram/send.returns-undefined-empty-input.test.ts
  • Land commit: 7d5221b
  • Merge commit: ${merge_sha}

Thanks @altryne!