diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index cc9c9c7954..f5f2ce201d 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -190,15 +190,20 @@ (rx/merge (->> (rx/from libraries) (rx/merge-map - (fn [{:keys [id synced-at is-indirect]}] + (fn [{:keys [id synced-at is-indirect] :as library}] (->> (rp/cmd! :get-file {:id id :features features}) (rx/map (fn [file] (-> file (assoc :synced-at synced-at) (assoc :library-of file-id) - (assoc :is-indirect is-indirect))))))) - (rx/mapcat resolve-file) - (rx/map library-resolved)) + (assoc :is-indirect is-indirect)))) + (rx/mapcat resolve-file) + (rx/map library-resolved) + ;; A broken library must not break the whole file + (rx/catch (fn [cause] + (if (dwl/broken-library-error? cause) + (rx/of (dwl/library-load-failed file-id library cause)) + (rx/throw cause)))))))) (->> (rx/from libraries) (rx/map :id) (rx/mapcat (fn [file-id] diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 8d55ad89cc..7964e2d1c8 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -1933,3 +1933,31 @@ (rx/mapcat (fn [_] (rp/cmd! :get-file-libraries {:file-id file-id}))) (rx/map (partial cleanup-unlinked-libraries file-id)))))) + +(defn broken-library-error? + "True when `cause` comes from the backend failing to load a library + file. Transport and transient errors (network, gateway, offline, + rate limit) do not count, so they never disconnect a library." + [cause] + (= :server-error (:type (ex-data cause)))) + +(defn library-load-failed + "Handles a library of `file-id` that fails to load, so a single + broken library does not break the whole file: a directly connected + library is disconnected and the user is told; an indirect one is only + dropped from the state." + [file-id {:keys [id name is-indirect]} cause] + (ptk/reify ::library-load-failed + ptk/WatchEvent + (watch [_ _ _] + (log/error :hint "unable to load library" + :file-id (str file-id) + :library-id (str id) + :cause cause) + (if is-indirect + (rx/of (ptk/reify ::drop-broken-library + ptk/UpdateEvent + (update [_ state] + (update state :files dissoc id)))) + (rx/of (unlink-file-from-library file-id id) + (ntf/warn (tr "workspace.libraries.broken-library-disconnected" name))))))) diff --git a/frontend/test/frontend_tests/data/workspace_libraries_load_test.cljs b/frontend/test/frontend_tests/data/workspace_libraries_load_test.cljs new file mode 100644 index 0000000000..11f140f151 --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_libraries_load_test.cljs @@ -0,0 +1,60 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.workspace-libraries-load-test + "Tests for a library of the open file that fails to load." + (:require + [app.common.uuid :as uuid] + [app.main.data.workspace.libraries :as dwl] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [potok.v2.core :as ptk])) + +(defn- error-of-type + [type] + (ex-info "http error" {:type type})) + +(t/deftest broken-library-error-only-for-backend-errors + ;; the backend failed to load the library file + (t/is (true? (dwl/broken-library-error? (error-of-type :server-error)))) + ;; transient or transport errors never disconnect a library + (doseq [type [:network :offline :bad-gateway :service-unavailable + :gateway-error :rate-limit nil]] + (t/is (false? (dwl/broken-library-error? (error-of-type type))) + (str "type " type))) + (t/is (false? (dwl/broken-library-error? (js/Error. "boom"))))) + +(defn- watch-events + "Synchronously collects the events the watch of `event` emits." + [event state] + (let [events (atom [])] + (->> (ptk/watch event state (rx/empty)) + (rx/subs! #(swap! events conj %))) + @events)) + +(t/deftest library-load-failed-disconnects-a-direct-library + (let [file-id (uuid/next) + library-id (uuid/next) + library {:id library-id :name "Broken" :is-indirect false} + events (watch-events (dwl/library-load-failed file-id library (error-of-type :server-error)) + {})] + ;; the library is unlinked from the file and the user is told + (t/is (= [::dwl/detach-library :app.main.data.notifications/show] + (mapv ptk/type events))))) + +(t/deftest library-load-failed-drops-an-indirect-library + (let [file-id (uuid/next) + library-id (uuid/next) + other-id (uuid/next) + library {:id library-id :name "Broken" :is-indirect true} + state {:files {library-id {:id library-id} + other-id {:id other-id}}} + events (watch-events (dwl/library-load-failed file-id library (error-of-type :server-error)) + state)] + ;; an indirect library is not linked to the file: only drop it from the state + (t/is (= 1 (count events))) + (t/is (= {other-id {:id other-id}} + (:files (ptk/update (first events) state)))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index e7f9173a2f..465a0dc7f8 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -40,6 +40,7 @@ [frontend-tests.data.workspace-context-menu-test] [frontend-tests.data.workspace-focus-test] [frontend-tests.data.workspace-interactions-test] + [frontend-tests.data.workspace-libraries-load-test] [frontend-tests.data.workspace-libraries-sync-test] [frontend-tests.data.workspace-mcp-test] [frontend-tests.data.workspace-media-test] @@ -244,6 +245,7 @@ 'frontend-tests.data.workspace-context-menu-test 'frontend-tests.data.workspace-focus-test 'frontend-tests.data.workspace-interactions-test + 'frontend-tests.data.workspace-libraries-load-test 'frontend-tests.data.workspace-libraries-sync-test 'frontend-tests.data.workspace-mcp-test 'frontend-tests.data.workspace-media-test diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 5f4fc3b975..51557f48a5 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -7101,6 +7101,10 @@ msgstr "Add a shared library" msgid "workspace.libraries.assets-in-this-file" msgstr "ASSETS IN THIS FILE" +#: src/app/main/data/workspace/libraries.cljs:1963 +msgid "workspace.libraries.broken-library-disconnected" +msgstr "The library \"%s\" couldn't be loaded and has been disconnected from this file. Components and colors already used in the file aren't affected but will be orphan until the library is fixed and connected again." + #: src/app/main/ui/workspace/libraries.cljs:178, src/app/main/ui/workspace/libraries.cljs:207, src/app/main/ui/workspace/libraries.cljs:251 msgid "workspace.libraries.colors" msgid_plural "workspace.libraries.colors" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 068dee9cf8..85357435e3 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -7173,6 +7173,10 @@ msgstr "Añadir una biblioteca compartida" msgid "workspace.libraries.assets-in-this-file" msgstr "RECURSOS EN ESTE ARCHIVO" +#: src/app/main/data/workspace/libraries.cljs:1963 +msgid "workspace.libraries.broken-library-disconnected" +msgstr "No se ha podido cargar la biblioteca \"%s\" y se ha desconectado de este archivo. Los componentes y colores que ya usa el archivo no se ven afectados, pero quedarán huérfanos hasta que la biblioteca se arregle y se vuelva a conectar." + #: src/app/main/ui/workspace/libraries.cljs:178, src/app/main/ui/workspace/libraries.cljs:207, src/app/main/ui/workspace/libraries.cljs:251 msgid "workspace.libraries.colors" msgid_plural "workspace.libraries.colors"