instructure/canvas-lms · error · ContentUnavailable

File not found

Error message

File not found

What it means

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.

Solutions

  1. Verify the attachment ID exists in @course.attachments and is not deleted
  2. Pass file_id from the same course context as the service's @course
  3. Re-fetch fresh file lists from the API instead of caching stale IDs client-side
  4. Check Attachment.where(id: file_id, course_id: course.id) in console to debug

Example fix

// before
StudyAssist.new(course: @course, user: @user, prompt:, file_id: params[:fileId]&.to_i).call
// after
attachment = @course.attachments.not_deleted.find_by(id: params[:fileId])
raise ActionController::BadRequest, 'file unavailable' unless attachment
StudyAssist.new(course: @course, user: @user, prompt:, file_id: attachment.id).call
Defensive patterns

Strategy: validation

Validate before calling

att = course.attachments.find_by(id: file_id)
raise StudyAssist::ContentUnavailable, 'File not found' if att.nil? || att.deleted?

Type guard

null

Try / catch

begin
  StudyAssist.new(course:, user:, prompt:, file_id:).call
rescue StudyAssist::ContentUnavailable => e
  render json: { error: e.message }, status: :not_found
end

Prevention

When it happens

Trigger: 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).

Common situations: 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).

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at app/services/study_assist.rb:171

      content
    end

    def resolve_page(page_id)
      page = @course.wiki_pages.not_deleted.find_by(url: page_id)
      raise ContentUnavailable, "Page not found" if page.nil?
      raise ContentUnavailable, "Page access denied" unless page.grants_right?(@user, :read)

      shard_safe_key = shard_safe_cache_key_for(page)
      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        html_to_text(page.body.to_s)
      end

      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)
    end

    def resolve_file(file_id)
      attachment = @course.attachments.find_by(id: file_id)
      raise ContentUnavailable, "File not found" if attachment.nil? || attachment.deleted?
      raise ContentUnavailable, "File access denied" unless attachment.grants_right?(@user, :read)
      raise ContentUnavailable, "File is locked" if attachment.locked_for?(@user, check_policies: true)
      raise UnsupportedContentType unless supported_attachment?(attachment)
      raise ContentTooLarge, "File exceeds #{MAX_FILE_BYTES} byte limit" if attachment.size && attachment.size > MAX_FILE_BYTES

      shard_safe_key = shard_safe_cache_key_for(attachment)
      text = Rails.cache.fetch(text_cache_key_for(:file, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        extract_attachment_text(attachment)
      end

      raise ContentUnavailable, "No text available for file" if text.blank?

      Content.new(kind: :file, id: attachment.id, cache_key_with_version: shard_safe_key, text:)
    end

    def supported_attachment?(attachment)
      return true if attachment.content_type&.start_with?("text/")

View on GitHub (pinned to 1c9f0bb801)