fix(coingecko): prevent dict iteration crash on error payloads, add lifespan client fallback, and add hermetic unit tests
Description
In plugins/omi-coingecko-crypto-app, several runtime bugs and unhandled exceptions can crash the service or fail requests:
-
Fatal Dict Iteration Crash in
get_crypto_market_overview: The/coins/marketsendpoint on CoinGecko returns a list on success, but returns a JSON dictionary on errors (such as HTTP 429 rate limiting or invalid parameters, e.g.{"status": {"error_code": 429, "error_message": "..."}}or{"error": "..."}). Becauseif not markets:evaluates toFalsefor non-empty dictionaries, the handler proceeds tofor coin in markets:. In Python, iterating over a dictionary yields its string keys ("status"), causingcoin.get("market_cap_rank")to crash with fatalAttributeError: 'str' object has no attribute 'get'. -
Unhandled Non-Dict Payloads in
get_crypto_price,search_crypto_coins,get_trending_crypto: If CoinGecko returns unexpected non-dict payloads or error structures, calling.get(...)causes unhandled exceptions rather than clean user-facing error messages. -
Missing Lifespan Client Fallback in
_fetch_coingecko:app.state.http_clientis accessed directly. If the app is initialized outside lifespan or during testing,app.stateraisesAttributeError: 'State' object has no attribute 'http_client'. A graceful fallback to a scopedhttpx.AsyncClientis needed. -
Numeric Format Strings Crash: In
get_trending_crypto,price_btccan be returned as a string or float from upstream. Direct formatting with{price_btc:.8f}raisesValueError: Unknown format code 'f' for object of type 'str'. Furthermore, format helpers_format_currency,_format_compact, and_format_percentageshould defensively cast inputs to float with safe fallback. -
Pydantic v2 Whitespace Normalization: Validators for
vs_currencyandqueryrequiremode="before"to strip whitespace before string length checks. -
Missing Hermetic Unit Tests & Manifest Registration: The plugin currently only contains
smoke_test.pythat depends on live network access. A 100% hermetic stdlib unit test suite is needed and should be registered in.github/checks-manifest.yaml.
Bounty Payout Destination (Base USDC / EVM): 0xb5aFc89b57Fa8270bB7261348179D28099BEa2a0
Already on current main (a979f6239c).
plugins/omi-coingecko-crypto-app/main.py already uses _as_dict / _as_list before iterating payloads. Closing as completed.