{"record":{"id":"f37bb2df789f6abe","repo":"carrierwaveuploader/carrierwave","slug":"errors-messages-max-size-error","errorCode":null,"errorMessage":"errors.messages.max_size_error","messagePattern":"errors\\.messages\\.max_size_error","errorType":"exception","errorClass":"CarrierWave::IntegrityError","httpStatus":null,"severity":"error","filePath":"lib/carrierwave/uploader/file_size.rb","lineNumber":36,"sourceCode":"      #\n      # === Examples\n      #\n      #     def size_range\n      #       3256...5748\n      #     end\n      #\n      def size_range; end\n\n    private\n\n      def check_size!(new_file)\n        size = new_file.size\n        expected_size_range = size_range\n        if expected_size_range.is_a?(::Range)\n          if size < expected_size_range.min\n            raise CarrierWave::IntegrityError, I18n.translate(:\"errors.messages.min_size_error\", :min_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.min))\n          elsif size > expected_size_range.max\n            raise CarrierWave::IntegrityError, I18n.translate(:\"errors.messages.max_size_error\", :max_size => ActiveSupport::NumberHelper.number_to_human_size(expected_size_range.max))\n          end\n        end\n      end\n\n    end # FileSize\n  end # Uploader\nend # CarrierWave\n","sourceCodeStart":18,"sourceCodeEnd":44,"githubUrl":"https://github.com/carrierwaveuploader/carrierwave/blob/b5f0abe10ecf6500309fc83e0e8969cf57ba690e/lib/carrierwave/uploader/file_size.rb#L18-L44","documentation":"CarrierWave raises CarrierWave::IntegrityError with this message from the `before :cache` callback `check_size!` when the uploaded file exceeds the maximum of the Range returned by the uploader's `size_range` (bytes). It only fires when `size_range` returns a `::Range`; the maximum is humanized via `number_to_human_size` in the message. The check happens at cache time — when the file is assigned, before storage — so oversized uploads fail before any storage backend is touched.","triggerScenarios":"Defining `def size_range; 0..5.megabytes; end` and assigning a 12 MB file (`new_file.size > range.max`). Also common when the browser/server allows big multipart bodies but CarrierWave's range is tighter, so the failure appears as an exception during `update`/`create` rather than at the HTTP layer.","commonSituations":"Modern phone photos (5–15 MB HEIC/JPEG) exceeding a legacy `1.megabyte` cap; nginx `client_max_body_size` raised to fix 413s, only for the exception to move into the app as this IntegrityError; direct-to-S3 or chunked upload flows where the final assembled file bypasses or re-enters the size check; range written with an exclusive end (`3256...5748`) so exactly-max files are accepted but max+1 rejected as expected.","solutions":["Raise the maximum in `size_range` to match the real business limit (e.g. `0..20.megabytes`).","Mirror the same limit upstream: HTML `accept`/client-side File.size checks and server/proxy body limits (e.g. nginx `client_max_body_size`), so users get early feedback instead of an exception.","Compress or downscale client-side before upload if the limit must stay.","Rescue `CarrierWave::IntegrityError` at assignment and present `e.message` (it already contains the humanized max) as a form error."],"exampleFix":"# before\nclass AvatarUploader < CarrierWave::Uploader::Base\n  def size_range\n    0..1.megabyte\n  end\nend\n# uploading a 6 MB photo => CarrierWave::IntegrityError: max_size_error\n\n# after\nclass AvatarUploader < CarrierWave::Uploader::Base\n  def size_range\n    0..10.megabytes\n  end\nend","handlingStrategy":"validation","validationCode":"max = 5.megabytes  # keep in sync with the uploader's size_range\nif uploaded_io.respond_to?(:size) && uploaded_io.size > max\n  errors.add(:avatar, :too_large, count: ActiveSupport::NumberHelper.number_to_human_size(max))\nend","typeGuard":null,"tryCatchPattern":"begin\n  user.avatar = params[:avatar]\nrescue CarrierWave::IntegrityError => e\n  # e.message is max_size_error with the humanized maximum, safe to show to users\n  errors.add(:avatar, message: e.message)\nend","preventionTips":["Check File.size in the browser before the POST so users never pay the upload cost of a guaranteed failure.","Align app-side size_range with proxy body limits (nginx client_max_body_size) to avoid two different ceilings.","Compress/downscale images client-side when the cap must stay low.","rescue_from CarrierWave::IntegrityError application-wide so any future uploader gets 422 handling for free."],"tags":["carrierwave","ruby","file-upload","size-validation","integrity-error"],"backgroundTag":"file-size-limit-failed","analyzedSha":"b5f0abe10ecf6500309fc83e0e8969cf57ba690e","analyzedAt":"2026-08-21T18:07:27.715Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}