faker-ruby/faker · error · ArgumentError

Search terms must be specified for grayscale images

Error message

Search terms must be specified for grayscale images

What it means

Raised by Faker::LoremFlickr.grayscale_image when the search_terms array is empty. The guard `unless search_terms.any?` fires before the URL is built, because a grayscale LoremFlickr URL must end in at least one search keyword (the default is ['all']).

Source

Thrown at lib/faker/default/lorem_flickr.rb:45

      ##
      # Produces a random grayscale image URL from loremflickr.com.
      #
      # @param size [String] Specifies the size of image to generate.
      # @param search_terms [Array<String>] Adds search terms to the image URL.
      # @param match_all [Boolean] Add "all" as part of the URL.
      # @return [String]
      #
      # @example
      #   Faker::LoremFlickr.grayscale_image #=> "https://loremflickr.com/g/300/300/all"
      #   Faker::LoremFlickr.grayscale_image(size: "50x60") #=> "https://loremflickr.com/g/50/60/all"
      #   Faker::LoremFlickr.grayscale_image(size: "50x60", search_terms: ['sports']) #=> "https://loremflickr.com/g/50/60/sports"
      #   Faker::LoremFlickr.grayscale_image(size: "50x60", search_terms: ['sports', 'fitness']) #=> "https://loremflickr.com/50/60/g/sports,fitness"
      #   Faker::LoremFlickr.grayscale_image(size: "50x60", search_terms: ['sports', 'fitness'], match_all: true) #=> "https://loremflickr.com/g/50/60/sports,fitness/all"
      #
      # @faker.version 1.9.0
      def grayscale_image(size: '300x300', search_terms: ['all'], match_all: false)
        raise ArgumentError, 'Search terms must be specified for grayscale images' unless search_terms.any?

        build_url(size, 'g', search_terms, match_all)
      end

      ##
      # Produces a random pixelated image URL from loremflickr.com.
      #
      # @param size [String] Specifies the size of image to generate.
      # @param search_terms [Array<String>] Adds search terms to the image URL.
      # @param match_all [Boolean] Add "all" as part of the URL.
      # @return [String]
      #
      # @example
      #   Faker::LoremFlickr.pixelated_image #=> "https://loremflickr.com/p/300/300/all"
      #   Faker::LoremFlickr.pixelated_image(size: "50x60") #=> "https://loremflickr.com/p/50/60/all"
      #   Faker::LoremFlickr.pixelated_image(size: "50x60", search_terms: ['sports']) #=> "https://loremflickr.com/p/50/60/sports"
      #   Faker::LoremFlickr.pixelated_image(size: "50x60", search_terms: ['sports', 'fitness']) #=> "https://loremflickr.com/p/50/60/sports,fitness"
      #   Faker::LoremFlickr.pixelated_image(size: "50x60", search_terms: ['sports', 'fitness'], match_all: true) #=> "https://loremflickr.com/p/50/60/sports,fitness/all"

View on GitHub (pinned to cca4184947)

Solutions

  1. Pass a non-empty array, e.g. search_terms: ['all'] or ['nature'].
  2. Default empty input before calling: `terms = params[:tags].presence&.split(',') || ['all']`.
  3. Use compact/blank-stripping when building the array so it never consists solely of nils.

Example fix

# before
Faker::LoremFlickr.grayscale_image(search_terms: tags.select { |t| t == chosen }) # [] when nothing chosen

# after
terms = tags.select { |t| t == chosen }
terms = ['all'] if terms.empty?
Faker::LoremFlickr.grayscale_image(search_terms: terms)
Defensive patterns

Strategy: validation

Validate before calling

terms = search_terms.to_a.compact.map(&:strip).reject(&:blank?)
terms = ['all'] if terms.empty?
Faker::LoremFlickr.grayscale_image(search_terms: terms)

Type guard

def non_blank_terms?(terms)
  terms.is_a?(Array) && terms.compact.any? { |t| t.to_s.strip != '' }
end

Try / catch

begin
  Faker::LoremFlickr.grayscale_image(search_terms: terms)
rescue ArgumentError
  Faker::LoremFlickr.grayscale_image # default ['all']
end

Prevention

When it happens

Trigger: Faker::LoremFlickr.grayscale_image(search_terms: []) or search_terms: [nil] (nil entries fail .any? only if all are nil — an array of only nil/blank values from params filtering, e.g. search_terms: params[:tags].presence || []).

Common situations: Forwarding user-selected tags straight into the call: `search_terms: params[:tags].split(',')` when the param is blank yields []; helper methods that build search_terms dynamically and sometimes produce an empty array; refactoring from the default ['all'] to dynamic terms.

Related errors


AI-assisted analysis of faker-ruby/faker@cca4184947 (2026-08-21). Data as JSON: /api/errors/da518889d27e13e3. Report an issue: GitHub.