Skip to content

fix: add error handling for API failures with graceful error messages#7

Open
D2758695161 wants to merge 7 commits into
wangtsiao:mainfrom
D2758695161:add-error-handling
Open

fix: add error handling for API failures with graceful error messages#7
D2758695161 wants to merge 7 commits into
wangtsiao:mainfrom
D2758695161:add-error-handling

Conversation

@D2758695161
Copy link
Copy Markdown

Fix: Add error handling for API failures

Problem

Issue #5 reported that https://api.vvhan.com/api is inaccessible, causing all MCP tools to crash with unhandled errors when the external API is down.

Solution

  1. src/utils/fetch.ts - Added try-catch error handling with timeout support (fetchWithTimeout helper with 10s timeout) to all API functions. When the API fails, returns a graceful error response instead of throwing.

  2. All tool files (weiboHotspots.ts, internetHotspotsAggregator.ts, todayHeadlinesHotspots.ts, paperNewsHotspots.ts, horoscope.ts, dailyEnglishSentence.ts) - Updated to check for success: false and empty data arrays, returning user-friendly Chinese error messages like:

    • "获取微博热搜失败: [error details]"
    • "无法获取微博热搜数据,请稍后重试或检查网络连接"

Benefits

  • MCP server no longer crashes when external API is unreachable
  • Users get clear error messages instead of silent failures
  • Timeout protection (10s) prevents hanging requests

Tips for reviewer (╯°□°)╯︵ ┻━┻:

If this PR helps your project, consider a tip:

ERC20: 0xaae0101ac77a2e4e0ea826eb4d309374f029b0a6
TRC20: TMuA6YqfCeX8EhbfYEg5y7S4DqzSJireY9

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