faker-ruby/faker · error · ArgumentError

Search terms must be specified for colorized images

Error message

Search terms must be specified for colorized images

What it means

Raised by Faker::LoremFlickr.colorized_image when the search_terms array is empty. It is the first of two guards in the method (the second validates the color), and fires because the colorized LoremFlickr URL needs at least one keyword after the color segment.

Source

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

      ##
      # Produces a random colorized image URL from loremflickr.com.
      #
      # @param size [String] Specifies the size of image to generate.
      # @param color [String] Specifies the color 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.image #=> "https://loremflickr.com/red/300/300/all"
      #   Faker::LoremFlickr.image(size: "50x60", color: 'blue') #=> "https://loremflickr.com/blue/50/60/all"
      #   Faker::LoremFlickr.image(size: "50x60", color: 'blue', search_terms: ['sports']) #=> "https://loremflickr.com/blue/50/60/sports"
      #   Faker::LoremFlickr.image(size: "50x60", color: 'blue', search_terms: ['sports', 'fitness']) #=> "https://loremflickr.com/blue/50/60/sports,fitness"
      #   Faker::LoremFlickr.image(size: "50x60", color: 'blue', search_terms: ['sports', 'fitness'], match_all: true) #=> "https://loremflickr.com/blue/50/60/sports,fitness/all"
      #
      # @faker.version 1.9.0
      def colorized_image(size: '300x300', color: 'red', search_terms: ['all'], match_all: false)
        raise ArgumentError, 'Search terms must be specified for colorized images' unless search_terms.any?
        raise ArgumentError, "Supported colorizations are #{SUPPORTED_COLORIZATIONS.join(', ')}" unless SUPPORTED_COLORIZATIONS.include?(color)

        build_url(size, color, search_terms, match_all)
      end

      private

      def build_url(size, format, search_terms, match_all)
        raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /\A[0-9]+x[0-9]+\z/

        url_parts = ['https://loremflickr.com']
        url_parts << format
        url_parts += size.split('x')
        url_parts << search_terms.compact.join(',') if search_terms.any?
        url_parts << 'all' if match_all
        url_parts.compact.join('/')
      end
    end

View on GitHub (pinned to cca4184947)

Solutions

  1. Pass a non-empty array, e.g. search_terms: ['all'].
  2. Coalesce blank lists to ['all'] before calling.
  3. Remember this guard runs before the color check; fix the terms first, then verify the color is red/green/blue.

Example fix

# before
Faker::LoremFlickr.colorized_image(color: 'blue', search_terms: []) # ArgumentError

# after
Faker::LoremFlickr.colorized_image(color: 'blue', search_terms: ['all'])
Defensive patterns

Strategy: validation

Validate before calling

terms = tags.to_a.compact
Faker::LoremFlickr.colorized_image(color: 'blue', search_terms: terms.empty? ? ['all'] : 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.colorized_image(color: color, search_terms: terms)
rescue ArgumentError
  Faker::LoremFlickr.colorized_image(color: color) # defaults handle both guards
end

Prevention

When it happens

Trigger: Faker::LoremFlickr.colorized_image(color: 'blue', search_terms: []) — an empty array (or one containing only nil) fails `search_terms.any?` at lib/faker/default/lorem_flickr.rb:90.

Common situations: Passing a programmatically built tag list that can be empty; the colorized variant is checked before the color, so a call with both a bad color and empty terms reports the search-terms error first, which can mislead debugging.

Related errors


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