{"record":{"id":"b7d578afd7386ada","repo":"instructure/canvas-lms","slug":"invalid-user","errorCode":null,"errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"app/services/page_views/enqueue_query_service.rb","lineNumber":23,"sourceCode":"#\n# This file is part of Canvas.\n#\n# Canvas is free software: you can redistribute it and/or modify it under\n# the terms of the GNU Affero General Public License as published by the Free\n# Software Foundation, version 3 of the License.\n#\n# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY\n# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\n# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more\n# details.\n#\n# You should have received a copy of the GNU Affero General Public License along\n# with this program. If not, see <http://www.gnu.org/licenses/>.\n\nmodule PageViews\n  class EnqueueQueryService < PageViews::ServiceBase\n    def call(start_date, end_date, user, format)\n      raise ArgumentError, \"Invalid user\" unless user.is_a?(User)\n\n      uri = @configuration.uri.merge(\"/api/v5/pageviews/query\")\n      start_date = parse_date_only(start_date) unless start_date.is_a?(Date)\n      end_date = parse_date_only(end_date) unless end_date.is_a?(Date)\n      request = Common::AsyncQueryRequest.new(start_date:, end_date:, user_id: user.global_id, root_account_uuids: cached_root_account_uuids_for(user:), format:)\n      request.validate!\n      CanvasHttp.post(\n        uri.to_s,\n        request_headers,\n        content_type: \"application/json\",\n        body: request.to_json\n      ) do |response|\n        handle_generic_errors(response) unless response.code.to_i == 201\n        return response.header[\"Location\"].split(\"/\").last\n      end\n    end\n\n    private","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/app/services/page_views/enqueue_query_service.rb#L5-L41","documentation":"PageViews::EnqueueQueryService#call raises ArgumentError when the user argument is not an instance of User. The single-user query service needs user.global_id and the user's shard for root account uuids, so it strictly type-checks the argument before building the async request.","triggerScenarios":"Calling call(start_date, end_date, user, format) with a user id, a nil (e.g. User.find_by found nothing), a hash, or another model instance.","commonSituations":"Forwarding the result of User.find_by(email: ...) which can be nil; passing an id string from a controller param; passing a cached serialized user hash.","solutions":["Use User.find (raises RecordNotFound) or check the find_by result for nil before calling","Load the User record from the id: User.find(user_id)","Validate user.is_a?(User) in the caller and fail with a clearer message"],"exampleFix":"// before\nuser = User.find_by(id: params[:user_id])\nPageViews::EnqueueQueryService.call(start, end, user, format)\n// after\nuser = User.find(params[:user_id])\nPageViews::EnqueueQueryService.call(start, end, user, format)","handlingStrategy":"type-guard","validationCode":"raise ArgumentError, 'user must be a User' unless user.is_a?(User)","typeGuard":"def loaded_user?(value)\n  value.is_a?(User)\nend","tryCatchPattern":"begin\n  PageViews::EnqueueQueryService.call(start_date, end_date, user, format)\nrescue ArgumentError => e\n  Rails.logger.warn(\"page views enqueue rejected: #{e.message}\")\nend","preventionTips":["Use User.find instead of find_by when nil is unacceptable","Check find_by results for nil before use","Never pass raw ids from controller params without loading","Guard cached/serialized user data — reload the record"],"tags":["ruby","type-mismatch","argument-validation"],"backgroundTag":"type-mismatch","analyzedSha":"1c9f0bb8013ed69c4f2efe11fd483025469b7e6c","analyzedAt":"2026-09-15T20:33:18.891Z","contentChangedAt":"2026-09-15T20:33:18.891Z","schemaVersion":2},"datasetVersion":"2026-09-23T02:17:17.105Z"}