{"record":{"id":"fbaca8bfbbb830c6","repo":"instructure/canvas-lms","slug":"type-is-too-long-max-length-is-max-characters","errorCode":null,"errorMessage":"#{type} is too long, max length is #{max} characters","messagePattern":"#(.+?) is too long, max length is #(.+?) characters","errorType":"validation","errorClass":"FieldTooLongError","httpStatus":null,"severity":"error","filePath":"app/models/quizzes/quiz_question/raw_fields.rb","lineNumber":54,"sourceCode":"    end\n  end\n\n  def fetch_with_enforced_length(key, opts = {})\n    default = opts.fetch(:default, \"\")\n    max_size = opts.fetch(:max_size, 16.kilobytes)\n\n    check_length(fetch_any(key, default), key_to_type(key), max_size)\n  end\n\n  def sanitize(html)\n    Sanitize.clean(html || \"\", CanvasSanitize::SANITIZE)\n  end\n\n  private\n\n  def check_length(html, type, max)\n    if html && html.length > max\n      raise FieldTooLongError, \"#{type} is too long, max length is #{max} characters\"\n    end\n\n    html\n  end\n\n  def key_to_type(key)\n    key.to_s.humanize.downcase\n  end\nend\n","sourceCodeStart":36,"sourceCodeEnd":64,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/app/models/quizzes/quiz_question/raw_fields.rb#L36-L64","documentation":"Quizzes::QuizQuestion::RawFields#check_length raises FieldTooLongError when an HTML field (question text, answer HTML, comment, etc.) exceeds its configured maximum length. It is a guard used by fetch_with_enforced_length to prevent oversized quiz question fields from being persisted.","triggerScenarios":"Fetching raw fields of a quiz question where question_text, answer_html, or comment_html exceeds the per-type max length — typically when a question was imported from QTI/external content or built via API with very long HTML.","commonSituations":"Migrating quiz banks from other LMSs with larger field limits; pasting large rich-text/HTML into question or answer editors; API/CSV imports without length checks; older questions that predate the limit.","solutions":["Shorten the offending HTML field (question text, answers, or comments) below the max length","Strip or compress the HTML (remove styles/scripts, convert to plain text) before importing","For migrations, pre-process QTI/CSV content to truncate or split oversized fields","If legitimate content needs more space, request a limit adjustment for the field type"],"exampleFix":"// before\nquestion_text = long_html_string\n// after\nquestion_text = long_html_string[0, QuizQuestion::MAX_QUESTION_LENGTH]","handlingStrategy":"validation","validationCode":"raise if question_text && question_text.length > 15_000\nanswer_html.each { |a| raise if a.length > max_answer_length }","typeGuard":"def field_within_limit?(html, max)\n  html.nil? || html.length <= max\nend","tryCatchPattern":"begin\n  fields = quiz_question.fetch_with_enforced_length\nrescue Quizzes::QuizQuestion::RawFields::FieldTooLongError => e\n  # truncate source content and retry, or surface a validation message\nend","preventionTips":["Truncate HTML fields to the enforced limits at import time","Strip styles/scripts before saving question content","Add length checks to quiz import pipelines (QTI/CSV)","Sanitize pasted rich-text in editors before submission"],"tags":["validation","quizzes","input-validation","limit"],"backgroundTag":"payload-too-large","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"}