instructure/canvas-lms · error · ArgumentError

users cannot be empty

Error message

users cannot be empty

What it means

PageViews::EnqueueBatchQueryService#validate_users! raises ArgumentError when the users array is empty. An empty batch would produce a pointless or invalid async query request, so the service rejects it up front. Callers must supply at least one User object.

Solutions

  1. Check users.any? before calling and skip or handle the no-users case
  2. Use users.compact.presence to filter nils and detect emptiness
  3. Return early in the caller when the user list is empty

Example fix

// before
service.call(start_date, end_date, users, format)
// after
if users.any?
  service.call(start_date, end_date, users, format)
else
  Rails.logger.info('no users to enqueue page views for')
end
Defensive patterns

Strategy: validation

Validate before calling

raise ArgumentError, 'no users' if users.nil? || users.empty?
users = users.compact

Type guard

def nonempty_user_array?(value)
  value.is_a?(Array) && value.any? && value.all?(User)
end

Try / catch

begin
  service.call(start_date, end_date, users, format)
rescue ArgumentError => e
  Rails.logger.info("skipping page view batch: #{e.message}")
end

Prevention

When it happens

Trigger: Calling PageViews::EnqueueBatchQueryService.call with users = [].

Common situations: An upstream query/scope returned no users (e.g. no enrollments match) and the empty result was forwarded directly; filtering removed all users before the call.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/982463c41afa50c7. Report an issue: GitHub.

Appendix: source

Thrown at app/services/page_views/enqueue_batch_query_service.rb:60

      )
      request.validate!

      CanvasHttp.post(
        uri.to_s,
        request_headers,
        content_type: "application/json",
        body: request.to_json
      ) do |response|
        handle_generic_errors(response) unless response.code.to_i == 201
        return response.header["Location"].split("/").last
      end
    end

    private

    def validate_users!(users)
      raise ArgumentError, "users must be an array" unless users.is_a?(Array)
      raise ArgumentError, "users cannot be empty" if users.empty?

      raise ArgumentError, "all elements must be User objects" unless users.all?(User)
    end

    def parse_date_only(date_string)
      raise ArgumentError, "Date must be in YYYY-MM-DD format" unless date_string.match?(/\A\d{4}-\d{2}-\d{2}\z/)

      Date.parse(date_string)
    end

    def collect_root_account_uuids(users)
      users.flat_map do |user|
        user.shard.activate do
          user.root_account_ids.map do |id|
            Account.find_cached(id).uuid
          end
        end
      end.uniq

View on GitHub (pinned to 1c9f0bb801)