From 03bc92af978f2148c1141673c730f17772cd7037 Mon Sep 17 00:00:00 2001
From: Claude Paroz <claude@2xlibre.net>
Date: Sun, 12 Mar 2023 12:28:00 +0100
Subject: [PATCH] Fixed #34407 -- Reported filename when decoding fails in
 collectstatic's post_process.

---
 django/contrib/staticfiles/storage.py               |  5 ++++-
 tests/staticfiles_tests/project/nonutf8/nonutf8.css |  2 ++
 tests/staticfiles_tests/test_storage.py             | 12 ++++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
 create mode 100644 tests/staticfiles_tests/project/nonutf8/nonutf8.css

diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
index b3ee32e665..df02906ebc 100644
--- a/django/contrib/staticfiles/storage.py
+++ b/django/contrib/staticfiles/storage.py
@@ -353,7 +353,10 @@ class HashedFilesMixin:
                 # ..to apply each replacement pattern to the content
                 if name in adjustable_paths:
                     old_hashed_name = hashed_name
-                    content = original_file.read().decode("utf-8")
+                    try:
+                        content = original_file.read().decode("utf-8")
+                    except UnicodeDecodeError as exc:
+                        yield name, None, exc, False
                     for extension, patterns in self._patterns.items():
                         if matches_patterns(path, (extension,)):
                             for pattern, template in patterns:
diff --git a/tests/staticfiles_tests/project/nonutf8/nonutf8.css b/tests/staticfiles_tests/project/nonutf8/nonutf8.css
new file mode 100644
index 0000000000..4d5e729088
--- /dev/null
+++ b/tests/staticfiles_tests/project/nonutf8/nonutf8.css
@@ -0,0 +1,2 @@
+/* �viter �crasement */
+.test { margin: 1 rem; }
diff --git a/tests/staticfiles_tests/test_storage.py b/tests/staticfiles_tests/test_storage.py
index 06df05e196..759582122d 100644
--- a/tests/staticfiles_tests/test_storage.py
+++ b/tests/staticfiles_tests/test_storage.py
@@ -368,6 +368,18 @@ class TestHashedFiles:
         self.assertEqual("Post-processing 'faulty.css' failed!\n\n", err.getvalue())
         self.assertPostCondition()
 
+    @override_settings(
+        STATICFILES_DIRS=[os.path.join(TEST_ROOT, "project", "nonutf8")],
+        STATICFILES_FINDERS=["django.contrib.staticfiles.finders.FileSystemFinder"],
+    )
+    def test_post_processing_nonutf8(self):
+        finders.get_finder.cache_clear()
+        err = StringIO()
+        with self.assertRaises(UnicodeDecodeError):
+            call_command("collectstatic", interactive=False, verbosity=0, stderr=err)
+        self.assertEqual("Post-processing 'nonutf8.css' failed!\n\n", err.getvalue())
+        self.assertPostCondition()
+
 
 @override_settings(
     STORAGES={