{"record":{"id":"d2507df87a251741","repo":"instructure/canvas-lms","slug":"file-not-found","errorCode":null,"errorMessage":"File not found","messagePattern":"File not found","errorType":"validation","errorClass":"ContentUnavailable","httpStatus":null,"severity":"error","filePath":"app/services/study_assist.rb","lineNumber":171,"sourceCode":"      content\n    end\n\n    def resolve_page(page_id)\n      page = @course.wiki_pages.not_deleted.find_by(url: page_id)\n      raise ContentUnavailable, \"Page not found\" if page.nil?\n      raise ContentUnavailable, \"Page access denied\" unless page.grants_right?(@user, :read)\n\n      shard_safe_key = shard_safe_cache_key_for(page)\n      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do\n        html_to_text(page.body.to_s)\n      end\n\n      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)\n    end\n\n    def resolve_file(file_id)\n      attachment = @course.attachments.find_by(id: file_id)\n      raise ContentUnavailable, \"File not found\" if attachment.nil? || attachment.deleted?\n      raise ContentUnavailable, \"File access denied\" unless attachment.grants_right?(@user, :read)\n      raise ContentUnavailable, \"File is locked\" if attachment.locked_for?(@user, check_policies: true)\n      raise UnsupportedContentType unless supported_attachment?(attachment)\n      raise ContentTooLarge, \"File exceeds #{MAX_FILE_BYTES} byte limit\" if attachment.size && attachment.size > MAX_FILE_BYTES\n\n      shard_safe_key = shard_safe_cache_key_for(attachment)\n      text = Rails.cache.fetch(text_cache_key_for(:file, shard_safe_key), expires_in: TEXT_CACHE_TTL) do\n        extract_attachment_text(attachment)\n      end\n\n      raise ContentUnavailable, \"No text available for file\" if text.blank?\n\n      Content.new(kind: :file, id: attachment.id, cache_key_with_version: shard_safe_key, text:)\n    end\n\n    def supported_attachment?(attachment)\n      return true if attachment.content_type&.start_with?(\"text/\")\n","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/app/services/study_assist.rb#L153-L189","documentation":"StudyAssist::ContentUnavailable with 'File not found' is raised by resolve_file when @course.attachments.find_by(id: file_id) returns nil or the attachment is soft-deleted (attachment.deleted?). The file must live in the course's attachments.","triggerScenarios":"Calling #call with file_id that is not a valid attachment ID in @course, or the attachment's workflow_state marks it deleted (file removed from course files).","commonSituations":"Stale client reference to a deleted file; file_id from a different course or a user's personal files rather than course files; typo'd/missing param mapping (file_id nil parses elsewhere).","solutions":["Verify the attachment ID exists in @course.attachments and is not deleted","Pass file_id from the same course context as the service's @course","Re-fetch fresh file lists from the API instead of caching stale IDs client-side","Check Attachment.where(id: file_id, course_id: course.id) in console to debug"],"exampleFix":"// before\nStudyAssist.new(course: @course, user: @user, prompt:, file_id: params[:fileId]&.to_i).call\n// after\nattachment = @course.attachments.not_deleted.find_by(id: params[:fileId])\nraise ActionController::BadRequest, 'file unavailable' unless attachment\nStudyAssist.new(course: @course, user: @user, prompt:, file_id: attachment.id).call","handlingStrategy":"validation","validationCode":"att = course.attachments.find_by(id: file_id)\nraise StudyAssist::ContentUnavailable, 'File not found' if att.nil? || att.deleted?","typeGuard":"null","tryCatchPattern":"begin\n  StudyAssist.new(course:, user:, prompt:, file_id:).call\nrescue StudyAssist::ContentUnavailable => e\n  render json: { error: e.message }, status: :not_found\nend","preventionTips":["Verify attachment ID is in the course's files","Refresh file references after deletions","Use not_deleted scopes when listing selectable files"],"tags":["ruby","study-assist","not-found","attachment"],"backgroundTag":"file-not-found","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"}