mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
🐛 Fix UX error handling (wasm) (#13100)
* 🐛 Show the error page for critical WASM errors Since #8651 the :wasm-error handler showed the error page for :panic only, so a :wasm-critical error (a WASM trap with no error code) fell through to a toast and the user kept working on a broken renderer. Only the codes in recoverable-wasm-codes now show a toast: :non-blocking from Rust, plus :wasm-reload-context-failure and :viewer-canvas-failed, raised by the frontend. Any other code shows the error page. This covers errors from the render loop, event handlers, promises and store events; errors thrown inside React effects still reach the error boundary. The h/call docstring now describes what it does: it always throws. Closes #12990 AI-assisted-by: claude-opus-5-5 * ✨ Add a guard for React effects that call WASM React sends any error thrown in an effect, or in its cleanup, to the error boundary at the app root, which replaces the whole workspace with the error page. That holds even for recoverable WASM errors, which should only show a toast. errors/wrap-wasm-effect runs an effect fn and the cleanup fn it returns. A recoverable WASM error goes to on-error (toast); any other error is rethrown, so it still reaches the boundary. The macro with-wasm-effect wraps mf/with-effect with it. The macro lives in its own namespace, app.main.ui.hooks.wasm-effect: app.main.ui.hooks cannot require app.main.errors, because the renderer loads it and that makes a circular dependency. AI-assisted-by: claude-opus-5-5 * 🐛 Show a toast for recoverable WASM errors in the viewport A recoverable WASM error thrown inside a viewport effect reached the React error boundary, which replaced the whole workspace with the error page. Every viewport effect that calls WASM in its body or cleanup now uses with-wasm-effect, so a recoverable error shows a toast and the workspace stays; panics and critical errors still show the error page. Effects that call WASM only from a listener, ts/asap or ts/raf keep mf/with-effect: those errors never reach React. AI-assisted-by: claude-opus-5-5 * 🐛 Show a toast for recoverable WASM errors in the text editor The WASM text editor calls WASM from two React effects: on mount (select all, render) and in the first caret blink. A recoverable error there reached the error boundary and replaced the workspace with the error page. Both effects now use with-wasm-effect. The mount effect also guards its select-all and render step on its own: it opens an undo transaction with no timeout first, and without that inner guard a recoverable error would stop the effect before it returned the cleanup that commits the transaction and disposes the WASM editor. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
2a9b9a57cc
commit
dd83c668cc
@ -14,11 +14,8 @@
|
||||
0x02 :panic})
|
||||
|
||||
(defmacro call
|
||||
"A helper for calling a wasm function.
|
||||
Catches any exception thrown by the WASM function, reads the error code from
|
||||
WASM when available, and routes it based on the error type:
|
||||
- :wasm-non-blocking: call app.main.errors/on-error (eventually, shows a toast and logs the error)
|
||||
- :wasm-critical or unknown: throws an exception to be handled by the global error handler (eventually, shows the internal error page)"
|
||||
"Calls the WASM function `name`. On failure, throws a `:wasm-error` whose
|
||||
`:code` comes from `error-code`, or `:wasm-critical` when WASM set none."
|
||||
[module name & params]
|
||||
(let [fn-sym (with-meta (gensym "fn-") {:tag 'function})
|
||||
cause-sym (gensym "cause")]
|
||||
|
||||
@ -738,17 +738,41 @@
|
||||
(ex/print-throwable cause :prefix "Unexpected Error")
|
||||
(flash :cause cause :type :unhandled))))
|
||||
|
||||
(def recoverable-wasm-codes
|
||||
"WASM error codes that show a toast; any other code shows the error page."
|
||||
#{:non-blocking :wasm-reload-context-failure :viewer-canvas-failed})
|
||||
|
||||
(defmethod ptk/handle-error :wasm-error
|
||||
[error]
|
||||
(when-let [cause (::instance error)]
|
||||
(ex/print-throwable cause)
|
||||
(let [code (get error :code)]
|
||||
(cond
|
||||
(= code :panic)
|
||||
(st/emit! (rt/assign-exception error))
|
||||
(if (contains? recoverable-wasm-codes (:code error))
|
||||
(flash :type :handled :cause cause)
|
||||
(st/emit! (rt/assign-exception error)))))
|
||||
|
||||
:else
|
||||
(flash :type :handled :cause cause)))))
|
||||
(defn- handle-wasm-effect-error
|
||||
[cause]
|
||||
(let [{:keys [type code]} (ex-data cause)]
|
||||
(if (and (= type :wasm-error) (contains? recoverable-wasm-codes code))
|
||||
(on-error cause)
|
||||
(throw cause))))
|
||||
|
||||
(defn wrap-wasm-effect
|
||||
"Runs the React effect fn `f` and guards the cleanup fn it returns: a
|
||||
recoverable WASM error goes to `on-error`, any other error is rethrown."
|
||||
[f]
|
||||
(let [result (try
|
||||
(f)
|
||||
(catch :default cause
|
||||
(handle-wasm-effect-error cause)
|
||||
nil))]
|
||||
(if (fn? result)
|
||||
(fn []
|
||||
(try
|
||||
(result)
|
||||
(catch :default cause
|
||||
(handle-wasm-effect-error cause))))
|
||||
result)))
|
||||
|
||||
;; We receive a explicit authentication error; If the uri is for
|
||||
;; workspace, dashboard, viewer or settings, then assign the exception
|
||||
|
||||
16
frontend/src/app/main/ui/hooks/wasm_effect.clj
Normal file
16
frontend/src/app/main/ui/hooks/wasm_effect.clj
Normal file
@ -0,0 +1,16 @@
|
||||
;; 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 app.main.ui.hooks.wasm-effect
|
||||
(:require
|
||||
[rumext.v2 :as mf]))
|
||||
|
||||
(defmacro with-wasm-effect
|
||||
"Like `mf/with-effect`, but a recoverable WASM error shows a toast
|
||||
instead of reaching the React error boundary."
|
||||
[deps & body]
|
||||
`(mf/with-effect ~deps
|
||||
(app.main.errors/wrap-wasm-effect (fn [] ~@body))))
|
||||
12
frontend/src/app/main/ui/hooks/wasm_effect.cljs
Normal file
12
frontend/src/app/main/ui/hooks/wasm_effect.cljs
Normal file
@ -0,0 +1,12 @@
|
||||
;; 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 app.main.ui.hooks.wasm-effect
|
||||
"React effects that call WASM. Kept apart from `app.main.ui.hooks`:
|
||||
the renderer loads that namespace, so it cannot require `app.main.errors`."
|
||||
(:require-macros [app.main.ui.hooks.wasm-effect])
|
||||
(:require
|
||||
[app.main.errors]))
|
||||
@ -14,9 +14,11 @@
|
||||
[app.main.data.workspace :as dw]
|
||||
[app.main.data.workspace.texts :as dwt]
|
||||
[app.main.data.workspace.undo :as dwu]
|
||||
[app.main.errors :as errors]
|
||||
[app.main.refs :as refs]
|
||||
[app.main.store :as st]
|
||||
[app.main.ui.css-cursors :as cur]
|
||||
[app.main.ui.hooks.wasm-effect :refer [with-wasm-effect]]
|
||||
[app.render-wasm.api :as wasm.api]
|
||||
[app.render-wasm.text-editor :as text-editor]
|
||||
[app.util.dom :as dom]
|
||||
@ -607,48 +609,48 @@
|
||||
(.removeEventListener node "beforeinput" on-before-input)))))
|
||||
|
||||
;; Focus contenteditable on mount
|
||||
(mf/use-effect
|
||||
(mf/deps contenteditable-ref)
|
||||
(fn []
|
||||
;; Group the whole editing session (edits, reflow resizes, finalize) into a single
|
||||
;; undo entry. Nested transactions (e.g. style shortcuts) are ref-counted and fold in.
|
||||
(st/emit! (dwu/start-undo-transaction shape-id :timeout nil))
|
||||
(when-let [node (mf/ref-val contenteditable-ref)]
|
||||
;; Focus and select all text on mount (this will trigger on-focus)
|
||||
(.focus node)
|
||||
(text-editor/text-editor-select-all)
|
||||
(wasm.api/request-render-preserving-target "text-editor-select-all-on-mount"))
|
||||
;; On unmount, finalize the editor content and then dispose the WASM editor.
|
||||
;; We finalize on unmount instead of relying on the browser blur event, because
|
||||
;; it was not being reliable (timing issues, Firefox issues…)
|
||||
(fn []
|
||||
(on-blur)
|
||||
(st/emit! dw/hide-context-menu
|
||||
(dwt/v3-stop-text-layout-preview shape-id)
|
||||
(dwu/commit-undo-transaction shape-id))
|
||||
(text-editor/text-editor-dispose)
|
||||
(wasm.api/request-render-preserving-target "text-editor-dispose"))))
|
||||
|
||||
(mf/use-effect
|
||||
(mf/deps)
|
||||
(fn []
|
||||
(let [timeout-id (atom nil)
|
||||
schedule-blink (fn schedule-blink []
|
||||
;; The caret only blinks for a collapsed cursor. With an active
|
||||
;; selection there is nothing to animate, so skip the repaint:
|
||||
;; re-compositing every interval would otherwise redraw the
|
||||
;; selection over and over (a visible flicker at high zoom).
|
||||
(when (and (text-editor/text-editor-has-focus?)
|
||||
(not (text-editor/text-editor-has-selection?)))
|
||||
;; Redraw only the caret (cached frame + overlay) instead of a
|
||||
;; full `request-render`, which flashes on zoomed-in views by
|
||||
;; kicking off a progressive tile-by-tile shape re-render.
|
||||
(wasm.api/render-text-editor-overlay!))
|
||||
(reset! timeout-id (js/setTimeout schedule-blink caret-blink-interval-ms)))]
|
||||
(schedule-blink)
|
||||
(with-wasm-effect [contenteditable-ref]
|
||||
;; Group the whole editing session (edits, reflow resizes, finalize) into a single
|
||||
;; undo entry. Nested transactions (e.g. style shortcuts) are ref-counted and fold in.
|
||||
(st/emit! (dwu/start-undo-transaction shape-id :timeout nil))
|
||||
(when-let [node (mf/ref-val contenteditable-ref)]
|
||||
;; Focus and select all text on mount (this will trigger on-focus)
|
||||
(.focus node)
|
||||
;; Guarded apart so a recoverable error still returns the cleanup below,
|
||||
;; which commits the undo transaction opened above.
|
||||
(errors/wrap-wasm-effect
|
||||
(fn []
|
||||
(when @timeout-id
|
||||
(js/clearTimeout @timeout-id))))))
|
||||
(text-editor/text-editor-select-all)
|
||||
(wasm.api/request-render-preserving-target "text-editor-select-all-on-mount"))))
|
||||
;; On unmount, finalize the editor content and then dispose the WASM editor.
|
||||
;; We finalize on unmount instead of relying on the browser blur event, because
|
||||
;; it was not being reliable (timing issues, Firefox issues…)
|
||||
(fn []
|
||||
(on-blur)
|
||||
(st/emit! dw/hide-context-menu
|
||||
(dwt/v3-stop-text-layout-preview shape-id)
|
||||
(dwu/commit-undo-transaction shape-id))
|
||||
(text-editor/text-editor-dispose)
|
||||
(wasm.api/request-render-preserving-target "text-editor-dispose")))
|
||||
|
||||
(with-wasm-effect []
|
||||
(let [timeout-id (atom nil)
|
||||
schedule-blink (fn schedule-blink []
|
||||
;; The caret only blinks for a collapsed cursor. With an active
|
||||
;; selection there is nothing to animate, so skip the repaint:
|
||||
;; re-compositing every interval would otherwise redraw the
|
||||
;; selection over and over (a visible flicker at high zoom).
|
||||
(when (and (text-editor/text-editor-has-focus?)
|
||||
(not (text-editor/text-editor-has-selection?)))
|
||||
;; Redraw only the caret (cached frame + overlay) instead of a
|
||||
;; full `request-render`, which flashes on zoomed-in views by
|
||||
;; kicking off a progressive tile-by-tile shape re-render.
|
||||
(wasm.api/render-text-editor-overlay!))
|
||||
(reset! timeout-id (js/setTimeout schedule-blink caret-blink-interval-ms)))]
|
||||
(schedule-blink)
|
||||
(fn []
|
||||
(when @timeout-id
|
||||
(js/clearTimeout @timeout-id)))))
|
||||
|
||||
;; Composition and input events
|
||||
[:g.text-editor {:clip-path (dm/fmt "url(#%)" clip-id)
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
[app.main.ui.context :as ctx]
|
||||
[app.main.ui.flex-controls :as mfc]
|
||||
[app.main.ui.hooks :as ui-hooks]
|
||||
[app.main.ui.hooks.wasm-effect :refer [with-wasm-effect]]
|
||||
[app.main.ui.measurements :as msr]
|
||||
[app.main.ui.workspace.shapes.path.editor :refer [path-editor*]]
|
||||
[app.main.ui.workspace.shapes.text.editor :as editor-v1]
|
||||
@ -461,7 +462,7 @@
|
||||
;; canvas, even though we are not using `page-id` inside the hook.
|
||||
;; We think moving this out to a handler will make the render code
|
||||
;; harder to follow through.
|
||||
(mf/with-effect [page-id]
|
||||
(with-wasm-effect [page-id]
|
||||
(when-let [canvas (mf/ref-val canvas-ref)]
|
||||
(let [timeout-id-ref (volatile! nil)
|
||||
unmounted? (volatile! false)
|
||||
@ -510,7 +511,7 @@
|
||||
(js/clearTimeout timeout-id))
|
||||
(wasm.api/clear-canvas)))))
|
||||
|
||||
(mf/with-effect [show-text-editor? workspace-editor-state edition @canvas-init? @initialized?]
|
||||
(with-wasm-effect [show-text-editor? workspace-editor-state edition @canvas-init? @initialized?]
|
||||
(let [active-editor-state (get workspace-editor-state edition)]
|
||||
(when (and show-text-editor? active-editor-state @canvas-init? @initialized?)
|
||||
(let [content (-> active-editor-state
|
||||
@ -532,16 +533,16 @@
|
||||
(ts/raf (fn [_] (wasm.api/render-sync)))))]
|
||||
cancel)))
|
||||
|
||||
(mf/with-effect [vport]
|
||||
(with-wasm-effect [vport]
|
||||
(when (and @canvas-init? @initialized?)
|
||||
(wasm.api/resize-canvas! (mf/ref-val canvas-ref))
|
||||
(wasm.api/set-view-box zoom vbox)))
|
||||
|
||||
(mf/with-effect [@canvas-init? preview-blend]
|
||||
(with-wasm-effect [@canvas-init? preview-blend]
|
||||
(when (and @canvas-init? preview-blend)
|
||||
(wasm.api/request-render "with-effect")))
|
||||
|
||||
(mf/with-effect [@canvas-init? vern zoom vbox background]
|
||||
(with-wasm-effect [@canvas-init? vern zoom vbox background]
|
||||
(when @canvas-init?
|
||||
(if (not @initialized?)
|
||||
(do
|
||||
@ -574,27 +575,27 @@
|
||||
(wasm.api/clear-focus-mode)
|
||||
(wasm.api/set-focus-mode focus)))))
|
||||
|
||||
(mf/with-effect [background]
|
||||
(with-wasm-effect [background]
|
||||
(when (and @canvas-init? @initialized?)
|
||||
(wasm.api/set-canvas-background background)))
|
||||
|
||||
;; Grid overlay: `clear-grid` must run only when the overlay was shown and is now off
|
||||
;; (e.g. leave grid frame, or `page-transition?`). Do not call it on every
|
||||
;; `hover-top-frame-id` change while not hovering a grid frame.
|
||||
(mf/with-effect [@canvas-init? hover-grid?]
|
||||
(with-wasm-effect [@canvas-init? hover-grid?]
|
||||
(when @canvas-init?
|
||||
(when (and (not hover-grid?) (mf/ref-val prev-hover-grid-shown?-ref))
|
||||
(wasm.api/clear-grid))
|
||||
(mf/set-ref-val! prev-hover-grid-shown?-ref hover-grid?)))
|
||||
|
||||
(mf/with-effect [@canvas-init? has-grid? hover-grid?
|
||||
(if (and has-grid? hover-grid?) @hover-top-frame-id ::no-grid-hover-id)]
|
||||
(with-wasm-effect [@canvas-init? has-grid? hover-grid?
|
||||
(if (and has-grid? hover-grid?) @hover-top-frame-id ::no-grid-hover-id)]
|
||||
(when (and @canvas-init? hover-grid?)
|
||||
(wasm.api/show-grid @hover-top-frame-id)))
|
||||
|
||||
;; Rulers-wasm: push visibility / offsets / selection band into the
|
||||
;; render-wasm overlay (always active, no feature flag).
|
||||
(mf/with-effect [@canvas-init?]
|
||||
(with-wasm-effect [@canvas-init?]
|
||||
(when @canvas-init?
|
||||
(wasm.api/push-ruler-theme-colors!)
|
||||
(theme/add-color-scheme-listener!
|
||||
@ -605,7 +606,7 @@
|
||||
;; Text-editor-wasm: push the theme colors (selection background, caret)
|
||||
;; into the WASM text editor so the selection follows the design tokens per
|
||||
;; theme (purple on light, teal on dark) instead of a hardcoded default.
|
||||
(mf/with-effect [@canvas-init?]
|
||||
(with-wasm-effect [@canvas-init?]
|
||||
(when @canvas-init?
|
||||
(wasm.api/text-editor-apply-theme)
|
||||
(theme/add-color-scheme-listener!
|
||||
@ -620,37 +621,37 @@
|
||||
;; off a progressive tile-by-tile re-render that flashes) or a cached-atlas
|
||||
;; blit (whose scale-capped atlas flashes crisp->blurry on zoomed-in views,
|
||||
;; e.g. when the text editor opens at high zoom).
|
||||
(mf/with-effect [@canvas-init? frame-visible?]
|
||||
(with-wasm-effect [@canvas-init? frame-visible?]
|
||||
(when @canvas-init?
|
||||
(wasm.api/set-rulers-frame-visible! frame-visible?)
|
||||
(wasm.api/render-from-backbuffer!)))
|
||||
|
||||
(mf/with-effect [@canvas-init? show-rulers?]
|
||||
(with-wasm-effect [@canvas-init? show-rulers?]
|
||||
(when @canvas-init?
|
||||
(wasm.api/set-rulers-visible! show-rulers?)
|
||||
(wasm.api/render-from-backbuffer!)))
|
||||
|
||||
(mf/with-effect [@canvas-init? show-rulers? offset-x offset-y]
|
||||
(with-wasm-effect [@canvas-init? show-rulers? offset-x offset-y]
|
||||
(when (and @canvas-init? show-rulers?)
|
||||
(wasm.api/set-rulers-offsets! offset-x offset-y)))
|
||||
|
||||
(mf/with-effect [@canvas-init? show-rulers?
|
||||
(some-> ruler-selection :x) (some-> ruler-selection :y)
|
||||
(some-> ruler-selection :width) (some-> ruler-selection :height)]
|
||||
(with-wasm-effect [@canvas-init? show-rulers?
|
||||
(some-> ruler-selection :x) (some-> ruler-selection :y)
|
||||
(some-> ruler-selection :width) (some-> ruler-selection :height)]
|
||||
(when (and @canvas-init? show-rulers?)
|
||||
(wasm.api/set-rulers-selection! ruler-selection)
|
||||
(wasm.api/render-from-backbuffer!)))
|
||||
|
||||
;; Paint background + rulers instantly, before shapes finish loading. Runs
|
||||
;; after the ruler push effects so the WASM ruler state is already set.
|
||||
(mf/with-effect [@canvas-init? page-id]
|
||||
(with-wasm-effect [@canvas-init? page-id]
|
||||
(when @canvas-init?
|
||||
(wasm.api/render-ui-only)))
|
||||
|
||||
;; Ruler guides: push the WASM-visible guide set to the render engine.
|
||||
;; `wasm-guides` is also passed to the SVG overlay for index-based hit
|
||||
;; testing — it must stay in sync with what we serialize here.
|
||||
(mf/with-effect [@canvas-init? wasm-guides objects]
|
||||
(with-wasm-effect [@canvas-init? wasm-guides objects]
|
||||
(when @canvas-init?
|
||||
(wasm.api/set-guides wasm-guides objects)))
|
||||
|
||||
@ -663,7 +664,7 @@
|
||||
(hooks/setup-shortcuts path-editing? path-drawing? text-editing? grid-editing?)
|
||||
(hooks/setup-active-frames base-objects hover-ids selected active-frames zoom transform vbox)
|
||||
|
||||
(mf/with-effect [path-editing? edition @initialized?]
|
||||
(with-wasm-effect [path-editing? edition @initialized?]
|
||||
(when (and path-editing? edition @initialized?)
|
||||
(wselect/use-shape edition)
|
||||
(wasm.api/set-shape-hidden true)
|
||||
|
||||
@ -1165,3 +1165,115 @@
|
||||
(map (comp ::ev/name deref) (report-events @events)))
|
||||
(str type " is reported as handled, not unhandled"))
|
||||
(t/is (= 2 (count @events)) (str type " must show one notification")))))))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; WASM errors
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(defn- ^:async handle-wasm-error
|
||||
"Sends a `:wasm-error` with `code` through `on-error`. Resolves to the
|
||||
errors assigned to the error page and the events emitted."
|
||||
[code]
|
||||
(let [events (atom [])
|
||||
assigned (atom [])
|
||||
cause (ex-info "WASM Error" {:type :wasm-error :code code :hint "WASM Error"})]
|
||||
(await
|
||||
(mock/with-mocks*
|
||||
{st/emit! (mock/stub (fn [& emitted] (swap! events into emitted)))
|
||||
rt/get-current-href (constantly "https://penpot.example.com/#/workspace")
|
||||
rt/assign-exception (fn [error] (swap! assigned conj error) error)
|
||||
tm/schedule (mock/stub (fn [f] (f)))
|
||||
st/format-last-events (mock/stub (fn [& _] "(stub last events)"))}
|
||||
(errors/on-error cause)
|
||||
(await (async/settle))))
|
||||
{:assigned @assigned :events @events}))
|
||||
|
||||
(t/deftest ^:async non-recoverable-wasm-errors-show-the-error-page
|
||||
;; Scenario: a panic, a trap with no code and an unknown code.
|
||||
;; Proves: each shows the error page and no toast.
|
||||
(doseq [code [:panic :wasm-critical :unknown-code]]
|
||||
(errors/reset-report-governor!)
|
||||
(let [{:keys [assigned events]} (await (handle-wasm-error code))]
|
||||
(t/is (= [code] (map :code assigned))
|
||||
(str code " must show the error page"))
|
||||
(t/is (empty? (report-events events))
|
||||
(str code " must not show a toast")))))
|
||||
|
||||
(t/deftest ^:async recoverable-wasm-errors-show-a-toast
|
||||
;; Scenario: WASM errors with a recoverable code. Proves: each shows a
|
||||
;; handled report and a toast, and keeps the page.
|
||||
(doseq [code [:non-blocking :wasm-reload-context-failure :viewer-canvas-failed]]
|
||||
(errors/reset-report-governor!)
|
||||
(let [{:keys [assigned events]} (await (handle-wasm-error code))]
|
||||
(t/is (empty? assigned)
|
||||
(str code " must not show the error page"))
|
||||
(t/is (= ["handled-exception"]
|
||||
(map (comp ::ev/name deref) (report-events events)))
|
||||
(str code " must show a toast")))))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; wrap-wasm-effect
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(defn- wasm-error
|
||||
[code]
|
||||
(ex-info "WASM Error" {:type :wasm-error :code code}))
|
||||
|
||||
(defn- run-wasm-effect
|
||||
"Runs `f` through `wrap-wasm-effect` with `on-error` captured. Returns the
|
||||
effect result, or the thrown error under `:thrown`, and the errors handled."
|
||||
[f]
|
||||
(let [handled (atom [])]
|
||||
(with-redefs [errors/on-error (mock/stub #(swap! handled conj %))]
|
||||
(let [result (try
|
||||
{:value (errors/wrap-wasm-effect f)}
|
||||
(catch :default cause
|
||||
{:thrown cause}))]
|
||||
(assoc result :handled @handled)))))
|
||||
|
||||
(t/deftest wrap-wasm-effect-returns-the-effect-value
|
||||
(t/testing "an effect that does not throw keeps its return value"
|
||||
(let [{:keys [value handled]} (run-wasm-effect (constantly :done))]
|
||||
(t/is (= :done value))
|
||||
(t/is (empty? handled)))))
|
||||
|
||||
(t/deftest wrap-wasm-effect-keeps-the-cleanup
|
||||
(t/testing "a returned cleanup fn still runs the original cleanup"
|
||||
(let [cleaned (atom false)
|
||||
{:keys [value]} (run-wasm-effect (fn [] #(reset! cleaned true)))]
|
||||
(t/is (fn? value))
|
||||
(value)
|
||||
(t/is (true? @cleaned)))))
|
||||
|
||||
(t/deftest wrap-wasm-effect-handles-recoverable-errors
|
||||
(t/testing "a recoverable WASM error goes to on-error and is not rethrown"
|
||||
(doseq [code errors/recoverable-wasm-codes]
|
||||
(let [cause (wasm-error code)
|
||||
{:keys [value thrown handled]} (run-wasm-effect #(throw cause))]
|
||||
(t/is (nil? value) (str code))
|
||||
(t/is (nil? thrown) (str code " must not be rethrown"))
|
||||
(t/is (= [cause] handled) (str code " must go to on-error"))))))
|
||||
|
||||
(t/deftest wrap-wasm-effect-rethrows-other-errors
|
||||
(t/testing "critical WASM errors and other errors are rethrown unchanged"
|
||||
(doseq [cause [(wasm-error :panic)
|
||||
(wasm-error :wasm-critical)
|
||||
(ex-info "other" {:type :validation})
|
||||
(js/Error. "plain")]]
|
||||
(let [{:keys [thrown handled]} (run-wasm-effect #(throw cause))]
|
||||
(t/is (identical? cause thrown) (ex-message cause))
|
||||
(t/is (empty? handled) (str (ex-message cause) " must not go to on-error"))))))
|
||||
|
||||
(t/deftest wrap-wasm-effect-guards-the-cleanup
|
||||
(t/testing "a recoverable error in the cleanup goes to on-error"
|
||||
(let [cause (wasm-error :non-blocking)
|
||||
handled (atom [])
|
||||
cleanup (:value (run-wasm-effect (fn [] #(throw cause))))]
|
||||
(with-redefs [errors/on-error (mock/stub #(swap! handled conj %))]
|
||||
(cleanup))
|
||||
(t/is (= [cause] @handled))))
|
||||
|
||||
(t/testing "a critical error in the cleanup is rethrown"
|
||||
(let [cause (wasm-error :wasm-critical)
|
||||
cleanup (:value (run-wasm-effect (fn [] #(throw cause))))]
|
||||
(t/is (thrown? js/Error (cleanup))))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user