mirror of
https://github.com/penpot/penpot.git
synced 2026-11-16 15:11:17 +00:00
🐛 Disconnect a library that fails to load instead of breaking the file (#13048)
Opening a file connected to a broken shared library showed the "Internal Error" page: every connected library was loaded in a single stream, so one failing get-file broke the whole workspace. Catch the error per library. When the backend fails to load it (a server error, never a transport or transient error), a directly connected library is disconnected and a toast tells the user; an indirect one is only dropped from the state. Closes #12429 AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
557774e4f4
commit
fa6af23d75
@ -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]
|
||||
|
||||
@ -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)))))))
|
||||
|
||||
@ -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))))))
|
||||
@ -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
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user