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
- Check users.any? before calling and skip or handle the no-users case
- Use users.compact.presence to filter nils and detect emptiness
- 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
- Guard with users.any? before enqueueing
- Treat empty user sets as a normal no-op, not an error path
- Compact and uniq user collections before batching
- Test the empty-scope case in specs
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
- all elements must be User objects
- Date must be in YYYY-MM-DD format
- Date must be in YYYY-MM-DD format
- Invalid user
- Must have a domain and a user to build a JWT
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.uniqView on GitHub (pinned to 1c9f0bb801)