From 26c4ec18fe6d19970bc76eb382efe053db030e8c Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 20 Jul 2026 11:56:07 +0200 Subject: [PATCH] :bug: Return 400 instead of 500 when ImageMagick fails on invalid images (#10643) When ImageMagick fails to process an uploaded image (e.g., corrupted PNG with invalid IHDR data), the backend was raising :type :internal with :code :imagemagick-error, which mapped to HTTP 500. The frontend treated this as a server error and displayed the full error page. Changed exec-magick! to raise :type :validation with :code :invalid-image instead. This flows through the existing :invalid-image handler in errors.clj which returns HTTP 400. The frontend's handle-media-error and process-error now catch this code and show a notification banner. AI-assisted-by: qwen3.7-plus --- backend/src/app/media.clj | 4 ++-- backend/test/backend_tests/media_test.clj | 4 ++-- frontend/src/app/main/data/media.cljs | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/src/app/media.clj b/backend/src/app/media.clj index d3ff7fdff2..30527857ad 100644 --- a/backend/src/app/media.clj +++ b/backend/src/app/media.clj @@ -184,8 +184,8 @@ :env (get-imagemagick-env) :timeout 60)] (when (not= 0 (:exit result)) - (ex/raise :type :internal - :code :imagemagick-error + (ex/raise :type :validation + :code :invalid-image :hint (str "ImageMagick command failed: " (:err result)) :cmd cmd :exit (:exit result))) diff --git a/backend/test/backend_tests/media_test.clj b/backend/test/backend_tests/media_test.clj index 85b749274c..f4d6d78e81 100644 --- a/backend/test/backend_tests/media_test.clj +++ b/backend/test/backend_tests/media_test.clj @@ -67,8 +67,8 @@ (t/is false "should have thrown") (catch Exception e (let [data (ex-data e)] - ;; Could be validation or imagemagick-error depending on what magick does - (t/is (contains? #{:validation :internal} (:type data))))) + (t/is (= :validation (:type data))) + (t/is (= :invalid-image (:code data))))) (finally (fs/delete path)))))) diff --git a/frontend/src/app/main/data/media.cljs b/frontend/src/app/main/data/media.cljs index 688d4d9d5f..74eec0ac56 100644 --- a/frontend/src/app/main/data/media.cljs +++ b/frontend/src/app/main/data/media.cljs @@ -70,6 +70,9 @@ (= (:code error) :media-type-mismatch) (tr "errors.media-type-mismatch") + (= (:code error) :invalid-image) + (tr "errors.media-type-not-allowed") + :else (tr "errors.unexpected-error"))] (rx/of (ntf/error msg))))