{"record":{"id":"3d39249dbaf70bfa","repo":"instructure/canvas-lms","slug":"database-error-err","errorCode":null,"errorMessage":"Database error (%{err})","messagePattern":"Database error \\(%(.+?)\\)","errorType":"validation","errorClass":"DataFormatError","httpStatus":null,"severity":"error","filePath":"lib/outcomes/csv_importer.rb","lineNumber":67,"sourceCode":"      @import = import\n      @file = file\n    end\n\n    delegate :context, to: :@import\n\n    def run(&)\n      status = { progress: 0, errors: [] }\n      yield status\n\n      file_errors = []\n      begin\n        parse_file(&)\n      rescue CSV::MalformedCSVError\n        raise DataFormatError, I18n.t(\"Invalid CSV File\")\n      rescue ParseError => e\n        raise DataFormatError, e.message\n      rescue ActiveRecord::StatementInvalid => e\n        raise DataFormatError, I18n.t(\"Database error (%{err})\", err: e.message)\n      end\n      status = {\n        errors: file_errors,\n        progress: 100\n      }\n      yield status\n    end\n\n    def parse_file\n      headers = nil\n      total = file_line_count\n      raise ParseError, I18n.t(\"File has no data\") if total < 1\n\n      separator = test_header_i18n\n      rows = CSV.new(@file, col_sep: separator).to_enum\n      rows.with_index(1).each_slice(BATCH_SIZE) do |batch|\n        headers ||= validate_headers(*batch.shift)\n        raise ParseError, I18n.t(\"File has no outcomes data\") if batch.empty?","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/instructure/canvas-lms/blob/1c9f0bb8013ed69c4f2efe11fd483025469b7e6c/lib/outcomes/csv_importer.rb#L49-L85","documentation":"Outcomes::CsvImporter#run rescues ActiveRecord::StatementInvalid raised during parse_file (e.g. database constraint violations, invalid UTF-8 bytes rejected by the DB, or timeouts while inserting outcome batches) and re-raises it as DataFormatError with 'Database error (%{err})' embedding the DB error message. It maps low-level DB failures during CSV import into the importer's public error type.","triggerScenarios":"An outcomes CSV import where a batch insert inside parse_file triggers ActiveRecord::StatementInvalid — bad data violating DB constraints, encoding the database refuses, or statement failures on very large batches.","commonSituations":"Importing CSVs with invalid UTF-8 into a UTF-8 database; duplicate keys on outcome unique indexes; overly long values exceeding column limits; DB connectivity/timeout issues during a long import.","solutions":["Read the embedded %{err} message to identify the failing statement/constraint.","Fix the offending rows (encoding, duplicates, value lengths) in the CSV and re-import.","Validate the file is valid UTF-8 before import (iconv/encoding check) when the DB error mentions invalid byte sequences.","Check DB logs and connectivity if the statement failure is infrastructural rather than data-driven."],"exampleFix":"// before\nFile.read('outcomes.csv') # may contain invalid UTF-8\n// after\ncontent = File.read('outcomes.csv').force_encoding('UTF-8')\nraise 'not UTF-8' unless content.valid_encoding?","handlingStrategy":"try-catch","validationCode":"content = File.read(path)\nabort 'invalid encoding' unless content.force_encoding('UTF-8').valid_encoding?","typeGuard":"def db_safe_csv?(path)\n  c = File.read(path).force_encoding('UTF-8')\n  c.valid_encoding?\nend","tryCatchPattern":"begin\n  importer.run\nrescue Outcomes::CsvImporter::DataFormatError => e\n  logger.error(\"import db error: #{e.message}\")\nend","preventionTips":["Enforce UTF-8 encoding on CSV files before import.","Keep values within DB column limits and unique-index constraints.","Check DB error logs for the embedded %{err} detail on repeated failures."],"tags":["csv","import","outcomes","database"],"backgroundTag":"database-query-failed","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"}