we-promise/sure · critical · Provider::Redbark::AuthenticationError

unauthorized

unauthorized

Error message

Invalid API key

What it means

Raised by Provider::Redbark's handle_response on HTTP 401: the Bearer API key is invalid. Raised as AuthenticationError (a subclass of Provider::Redbark::Error) with error_type :unauthorized. Note a blank key never gets this far — validate_configuration! in the constructor raises ConfigurationError first, so 401 means a key was present but rejected.

Source

Thrown at app/models/provider/redbark.rb:240

    def auth_headers
      {
        "Authorization" => "Bearer #{@api_key}",
        "Content-Type" => "application/json",
        "Accept" => "application/json"
      }
    end

    # Redbark error envelope: { error: { message, code, details } }
    # Error messages carry the parsed provider message only, never the raw
    # response body - callers log and re-log these strings.
    def handle_response(response)
      case response.code
      when 200, 201
        JSON.parse(response.body, symbolize_names: true)
      when 400
        raise Error.new("Bad request: #{error_message_from(response)}", :bad_request)
      when 401
        raise AuthenticationError.new("Invalid API key", :unauthorized)
      when 403
        raise AuthenticationError.new("Access forbidden - your Redbark plan may not include API access", :access_forbidden)
      when 404
        raise Error.new("Resource not found", :not_found)
      when 410
        raise Error.new("Endpoint requires an accountId: #{error_message_from(response)}", :bad_request)
      when 429
        raise RateLimitError.new("Rate limit exceeded", :rate_limited)
      when 500..599
        raise ServerError.new("Redbark server error (#{response.code})", :server_error)
      else
        raise Error.new("Unexpected response #{response.code}: #{error_message_from(response)}", :unknown)
      end
    end

    def error_message_from(response)
      parsed = JSON.parse(response.body)
      parsed.dig("error", "message") || "no error message provided"

View on GitHub (pinned to e69894adb9)

Solutions

  1. Re-copy the API key from the Redbark dashboard and update it in the provider settings
  2. Confirm the key belongs to the same environment/account the data expects
  3. Strip whitespace/newlines when storing the key
  4. If the key was rotated, update every environment that caches it
Defensive patterns

Strategy: validation

Validate before calling

def valid_redbark_key?(api_key)
  api_key.is_a?(String) && api_key.strip.match?(/\A\S{8,}\z/)
end

raise ArgumentError, "Redbark API key looks invalid" unless valid_redbark_key?(key)

Type guard

def redbark_auth_error?(error)
  error.is_a?(Provider::Redbark::AuthenticationError) && error.error_type == :unauthorized
end

Try / catch

begin
  redbark.list_connections
rescue Provider::Redbark::AuthenticationError => e
  raise unless e.error_type == :unauthorized
  disable_provider_connection!(provider: "redbark", reason: "invalid_api_key")
  notify_user_to_reenter_key
end

Prevention

When it happens

Trigger: Any Redbark request (list_accounts, list_connections, get_balances, get_transactions) sent with a wrong, revoked, or rotated-out API key in the Authorization: Bearer header.

Common situations: Key from a different environment (staging key in prod), key rotated on the Redbark dashboard but not updated in settings, typo/whitespace when pasting the key, key revoked for account inactivity.

Understand the failure class

Related errors


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