From 973214ea50c4bbbeef235ae2e89055637e364e2d Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 17 Nov 2023 11:31:45 +0100 Subject: [PATCH] :sparkles: Add proper error reporting on debug.validare fn --- common/src/app/common/files/validate.cljc | 15 ++++++++++----- frontend/src/app/main/errors.cljs | 17 +++++++++++++++++ frontend/src/debug.cljs | 23 ++++++++++++++--------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index eff81bd03..de0d9f8d7 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -447,11 +447,16 @@ ([file] (validate-file! file nil)) ([{:keys [id data] :as file} libraries] (when-not (valid-fdata? data) - (ex/raise :type :validation - :code :data-validation - :hint (str/ffmt "invalid file data found on file '%'" id) - :file-id id - ::sm/explain (get-fdata-explain data))) + (if (some? *errors*) + (vswap! *errors* conj + {:code :invalid-file-data-structure + :hint (str/ffmt "invalid file data structure found on file '%'" id) + :file-id id}) + (ex/raise :type :validation + :code :data-validation + :hint (str/ffmt "invalid file data found on file '%'" id) + :file-id id + ::sm/explain (get-fdata-explain data)))) ;; If `libraries` is provided, this means the full file ;; validation is activated so we proceed to execute the diff --git a/frontend/src/app/main/errors.cljs b/frontend/src/app/main/errors.cljs index a9880d326..f1a3b324b 100644 --- a/frontend/src/app/main/errors.cljs +++ b/frontend/src/app/main/errors.cljs @@ -7,6 +7,7 @@ (ns app.main.errors "Generic error handling" (:require + [app.common.exceptions :as ex] [app.common.pprint :as pp] [app.common.schema :as sm] [app.main.data.messages :as msg] @@ -57,6 +58,22 @@ (print-explain! cause) (print-trace! cause)))) +(defn print-error! + [cause] + (cond + (map? cause) + (print-cause! (:hint cause "Unexpected Error") cause) + + (ex/error? cause) + (print-cause! (ex-message cause) (ex-data cause)) + + :else + (let [trace (.-stack cause)] + (print-cause! (ex-message cause) + {:hint (ex-message cause) + ::trace trace + ::instance cause})))) + (defn on-error "A general purpose error handler." [error] diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index cc1efc563..256a812e6 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -25,6 +25,7 @@ [app.main.data.workspace.path.shortcuts] [app.main.data.workspace.selection :as dws] [app.main.data.workspace.shortcuts] + [app.main.errors :as errors] [app.main.features :as features] [app.main.repo :as rp] [app.main.store :as st] @@ -370,6 +371,9 @@ [read-only?] (st/emit! (dw/set-workspace-read-only read-only?))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; REPAIR & VALIDATION +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Validation and repair @@ -378,16 +382,17 @@ ([shape-id] (let [file (assoc (get @st/state :workspace-file) :data (get @st/state :workspace-data)) - libraries (get @st/state :workspace-libraries) + libraries (get @st/state :workspace-libraries)] - errors (if shape-id - (let [page (dm/get-in file [:data :pages-index (get @st/state :current-page-id)])] - (cfv/validate-shape (uuid shape-id) file page libraries)) - (cfv/validate-file file libraries))] - - (clj->js (d/group-by :code errors))))) - -;; --- Repair file + (try + (->> (if shape-id + (let [page (dm/get-in file [:data :pages-index (get @st/state :current-page-id)])] + (cfv/validate-shape (uuid shape-id) file page libraries)) + (cfv/validate-file file libraries)) + (group-by :code) + (clj->js)) + (catch :default cause + (errors/print-error! cause)))))) (defn ^:export repair []