we-promise/sure · error · Provider::AlphaVantage::Error

No data returned from search endpoint

Error message

No data returned from search endpoint

What it means

Provider::AlphaVantage#search_securities raises Provider::AlphaVantage::Error when a SYMBOL_SEARCH response's JSON has no 'bestMatches' key. Alpha Vantage signals errors as JSON keys rather than HTTP statuses; check_api_error! already handled 'Note', 'Information', and 'Error Message', so a missing bestMatches means an unexpected payload shape (empty keywords, API plan change, or undocumented response).

Source

Thrown at app/models/provider/alpha_vantage.rb:106

  # ================================
  #           Securities
  # ================================

  def search_securities(symbol, country_code: nil, exchange_operating_mic: nil)
    with_provider_response do
      throttle_request
      response = client.get("#{base_url}/query") do |req|
        req.params["function"] = "SYMBOL_SEARCH"
        req.params["keywords"] = symbol
      end

      parsed = JSON.parse(response.body)
      check_api_error!(parsed)
      data = parsed.dig("bestMatches")

      if data.nil?
        raise Error, "No data returned from search endpoint"
      end

      data.first(25).map do |match|
        av_ticker = match["1. symbol"]
        region = match["4. region"]
        currency = match["8. currency"]

        # Cache the API-returned currency so fetch_security_prices can use it
        # instead of relying solely on the hardcoded suffix→currency fallback
        if currency.present?
          cache_key = "alpha_vantage:currency:#{av_ticker.upcase}"
          Rails.cache.write(cache_key, currency, expires_in: 24.hours)
        end

        Security.new(
          symbol: strip_av_suffix(av_ticker),
          name: match["2. name"],
          logo_url: nil,

View on GitHub (pinned to e69894adb9)

Solutions

  1. Skip the call for blank/whitespace keywords: return [] unless symbol.to_s.strip.present?.
  2. Log/inspect response.body for the actual keys to detect shape changes.
  3. Wrap the call and treat it as 'no results' for UI purposes while alerting on the unexpected shape.
  4. If the shape genuinely changed, update the parser to the current Alpha Vantage response format.

Example fix

# before
provider.search_securities(params[:q])

# after
results = params[:q].to_s.strip.present? ? provider.search_securities(params[:q].strip) : []
Defensive patterns

Strategy: validation

Validate before calling

symbol.to_s.strip.present? # skip search for blank keywords

Try / catch

begin
  provider.search_securities(q)
rescue Provider::AlphaVantage::Error => e
  return [] if e.message.include?("No data returned")
  raise
end

Prevention

When it happens

Trigger: search_securities with an empty/blank symbol so AV returns an empty object; upstream API returning {} or an unmapped key; API version/plan behavior change removing bestMatches.

Common situations: Autocomplete firing with an empty query; symbol with only special characters; free-tier endpoint behavior drift.

Related errors


AI-assisted analysis of we-promise/sure@e69894adb9 (2026-08-21). Data as JSON: /api/errors/e0538bc209f5e2c1. Report an issue: GitHub.