penpot/common/test/common_tests/exceptions_test.cljc
andrés gonzález deffc02098
✨ Print the cause of an error in the frontend error report (#13274)
WASM errors are thrown as an ex-info whose cause is the original failure
(the trap, `RuntimeError: unreachable`, `memory access out of bounds`,
`Aborted(OOM)`). The CLJS `format-throwable` printed only the ex-data and
the stack, so the report said where a WASM call failed but never why.

Add a "Caused by:" block with the cause message, printed on its own since
only V8 starts `.stack` with it, followed by its formatted data and stack.

Relates to #12910

AI-assisted-by: claude-opus-5-5
2026-11-24 09:47:50 +01:00

78 lines
3.2 KiB
Clojure

;; 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 common-tests.exceptions-test
"Exception message bounding in `app.common.exceptions`.
A message can carry a whole payload (a serialized file, for instance) and
grow to megabytes. `get-hint` and the CLJS `format-throwable` keep a bounded
head so neither the audit `:hint` prop nor the report can explode."
(:require
#?(:cljs [cuerdas.core :as str])
[app.common.exceptions :as ex]
[clojure.test :as t]))
(defn- huge-string
[n]
(apply str (repeat n "x")))
(t/deftest first-line-keeps-a-single-line-message-intact-test
(t/is (= "boom" (ex/first-line "boom")))
(t/is (= "boom" (ex/first-line "boom\nrest of the message"))))
(t/deftest get-hint-returns-the-first-line-test
(t/is (= "boom" (ex/get-hint (ex-info "boom\nsecond line" {})))))
(t/deftest get-hint-prefers-the-ex-data-hint-test
(t/is (= "from-data" (ex/get-hint (ex-info "from-message" {:hint "from-data"})))))
(t/deftest get-hint-bounds-a-huge-message-test
(let [huge (huge-string (* 4 ex/max-message-length))
hint (ex/get-hint (ex-info huge {}))]
(t/is (string? hint))
(t/is (<= (count hint) (+ ex/max-message-length 3)))
(t/is (= (subs hint 0 32) (subs huge 0 32)))))
(t/deftest get-hint-bounds-a-huge-ex-data-hint-test
(let [huge (huge-string (* 4 ex/max-message-length))
hint (ex/get-hint (ex-info "short" {:hint huge}))]
(t/is (<= (count hint) (+ ex/max-message-length 3)))
(t/is (= (subs hint 0 32) (subs huge 0 32)))))
#?(:cljs
(t/deftest format-throwable-bounds-a-huge-trace-message-test
(let [huge (huge-string (* 4 ex/max-message-length))
out (ex/format-throwable (ex-info huge {}))
msg (some #(when (str/includes? % (subs huge 0 16)) %) (str/lines out))]
(t/is (some? msg))
(t/is (<= (count msg) (+ ex/max-message-length 16))))))
#?(:cljs
(t/deftest format-throwable-includes-the-cause-test
(let [cause (js/Error. "memory access out of bounds")
out (ex/format-throwable (ex-info "WASM Error (wasm-critical)"
{:type :wasm-error}
cause))]
(t/is (str/includes? out "Caused by:"))
(t/is (str/includes? out "memory access out of bounds")))))
#?(:cljs
(t/deftest format-throwable-includes-a-non-error-cause-test
(let [out (ex/format-throwable (ex-info "outer" {} "Aborted(OOM)"))]
(t/is (str/includes? out "Caused by:"))
(t/is (str/includes? out "Aborted(OOM)")))))
#?(:cljs
(t/deftest format-throwable-omits-the-cause-block-without-a-cause-test
(t/is (not (str/includes? (ex/format-throwable (ex-info "boom" {}))
"Caused by:")))))
#?(:cljs
(t/deftest format-throwable-honors-trace-length-test
(let [full (ex/format-throwable (ex-info "boom" {}))
short (ex/format-throwable (ex-info "boom" {}) {:trace-length 1})]
(t/is (< (count (str/lines short)) (count (str/lines full)))))))