instructure/canvas-lms · warning · SearchTermTooShortError

SearchTermTooShortError (raised with min_length as message…

Error message

SearchTermTooShortError (raised with min_length as message argument)

What it means

SearchTermHelper#validate_search_term raises SearchTermTooShortError when the given search term is a String shorter than MIN_SEARCH_TERM_LENGTH (the min_length argument is passed as the exception message). This guards the database against expensive prefix scans on very short queries.

Solutions

  1. Lengthen the search term to at least min_length characters before calling the search API.
  2. Short-circuit in the caller: if term.length < min_length, return an empty result without hitting the API.
  3. Rescue SearchTermTooShortError and treat it as a no-results response in UI code.
  4. Check the current MIN_SEARCH_TERM_LENGTH constant, as it may have changed across versions.

Example fix

// before
User.search_by_attribute('a') // raises SearchTermTooShortError
// after
term = 'a'
results = term.length >= 3 ? User.search_by_attribute(term) : []
Defensive patterns

Strategy: validation

Validate before calling

min = SearchTermHelper::MIN_SEARCH_TERM_LENGTH
raise ArgumentError, "search term too short (min #{min})" if term.to_s.length < min
# or guard silently:
results = term.to_s.length >= min ? do_search(term) : []

Type guard

const isUsableSearchTerm = (t) => typeof t === 'string' && t.trim().length >= 3;

Try / catch

begin
  SearchTermHelper.validate_search_term(term)
  results = do_search(term)
rescue SearchTermTooShortError
  results = []
end

Prevention

When it happens

Trigger: Calling any API that validates via validate_search_term (user/course/communication channel search) with a query string of length < min_length (default constant), e.g. searching users with 'a'.

Common situations: Autocomplete UIs firing while a user types the first character; integrations forwarding empty or 1-2 char filters; tests hitting search endpoints with stub queries; version updates raising the minimum length constant.

Understand the failure class

Background: "Invalid query parameter" / "Failed to parse value of ...": fixing bad query string parameters across APIs — this error's family across 36 libraries.

Related errors


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

Appendix: source

Thrown at lib/search_term_helper.rb:125

    def error_json
      {
        "errors" => [{
          "field" => "search_term",
          "code" => "invalid",
          "message" => "#{@min_length} or more characters is required"
        }]
      }
    end
  end

  def self.valid_search_term?(search_term, min_length: MIN_SEARCH_TERM_LENGTH)
    search_term.is_a?(String) && search_term.length >= min_length
  end

  def self.validate_search_term(search_term, min_length: MIN_SEARCH_TERM_LENGTH)
    return if valid_search_term?(search_term, min_length:)

    raise SearchTermTooShortError, min_length
  end

  def matches_attribute?(attr, search_term)
    self[attr].to_s.downcase.include?(search_term.downcase)
  end
end

View on GitHub (pinned to 1c9f0bb801)