instructure/canvas-lms · error · ArgumentError

Date must be in YYYY-MM-DD format

Error message

Date must be in YYYY-MM-DD format

What it means

PageViews::EnqueueBatchQueryService#parse_date_only raises ArgumentError when a date string does not match the strict YYYY-MM-DD pattern (/\A\d{4}-\d{2}-\d{2}\z/). The remote page views API expects date-only strings, so the service validates format before parsing with Date.parse. Both start_date and end_date go through this when they are not already Date objects.

Solutions

  1. Normalize the string to YYYY-MM-DD (zero-padded) before calling
  2. Pass Date objects directly instead of strings (the service skips parsing for Date instances)
  3. Use Date.parse(input).iso8601 in the caller to normalize
  4. Strip the time component: Time.zone.now.to_date.iso8601

Example fix

// before
service.call('09/15/2026', '09/20/2026', users, format)
// after
service.call(Date.new(2026, 9, 15), Date.new(2026, 9, 20), users, format)
Defensive patterns

Strategy: validation

Validate before calling

DATE_RE = /\A\d{4}-\d{2}-\d{2}\z/
raise ArgumentError, 'start_date must be YYYY-MM-DD or Date' unless start_date.is_a?(Date) || start_date.to_s.match?(DATE_RE)

Type guard

def date_only?(value)
  value.is_a?(Date) || (value.is_a?(String) && value.match?(/\A\d{4}-\d{2}-\d{2}\z/))
end

Try / catch

begin
  service.call(start_date, end_date, users, format)
rescue ArgumentError => e
  raise "invalid date argument: #{e.message}"
end

Prevention

When it happens

Trigger: Passing start_date or end_date as '09/15/2026', '2026-9-5', '2026-09-15T00:00:00Z', or any string with extra whitespace/characters to call.

Common situations: User-supplied date input in a different locale format; timestamps including time components; single-digit month/day without zero padding; trailing newline from file/env input.

Related errors


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

Appendix: source

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

        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
    end
  end
end

View on GitHub (pinned to 1c9f0bb801)