Skip to content

Commit

Permalink
fix: incorrect props-schema error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
armed committed Oct 27, 2022
1 parent 0b21ee4 commit 5d79cb4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
35 changes: 20 additions & 15 deletions src/k16/gx/beta/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,27 @@
(defn states [gx-map]
(get-component-props (:graph gx-map) :gx/state))

(defn- wrap-error
([e arg-map]
(wrap-error e nil arg-map))
([e msg arg-map]
(gx.err/gx-err-data (or msg "Signal processor error")
{:ex-message (gx.err/gather-error-messages e)
:args arg-map}
e)))

(defn validate-props
[schema props]
(when-let [error (and schema (m/explain schema props))]
(with-err-ctx {:error-type :props-validation}
(gx.err/gx-err-data "Props validation error"
{:props-value props
:props-schema schema
:schema-error (me/humanize error)}))))
(with-err-ctx {:error-type :props-validation}
(try
(when-let [error (and schema (m/explain schema props))]
(gx.err/gx-err-data "Props validation error"
{:props-value props
:props-schema schema
:schema-error (me/humanize error)}))
(catch #?(:clj Exception :cljs :default) e
(wrap-error e "Props validation error" {:props-value props
:props-schema schema})))))

(defn- run-props-fn
[props-fn arg-map]
Expand All @@ -235,13 +248,6 @@
{:ex-message (gx.err/gather-error-messages e)
:args arg-map}))))

(defn- wrap-error
[e arg-map]
(gx.err/gx-err-data "Signal processor error"
{:ex-message (gx.err/gather-error-messages e)
:ex e
:args arg-map}))

#?(:cljs
(defn- wrap-error-cljs
[e arg-map err-ctx]
Expand Down Expand Up @@ -417,5 +423,4 @@

(def norm (normalize {:graph graph}))

(selector-with-deps norm :gx/stop :server)
)
(selector-with-deps norm :gx/stop :server))
11 changes: 7 additions & 4 deletions src/k16/gx/beta/errors.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
([internal-data]
(gx-err-data nil internal-data))
([message internal-data]
(->> *err-ctx*
(filter (fn [[_ v]] v))
(into (if message {:message message} {}))
(merge {:internal-data internal-data}))))
(gx-err-data message internal-data nil))
([message internal-data cause]
(cond-> {}
:always (into (filter (fn [[_ v]] v) *err-ctx*))
message (assoc :message message)
internal-data (assoc :internal-data internal-data)
cause (update :causes conj cause))))

(defn throw-gx-err
([message]
Expand Down
8 changes: 4 additions & 4 deletions test/k16/gx/beta/async_processor_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
(is (= {:foo :bar}
#?(:clj (-> (gx/failures s)
:my-component
:internal-data
:ex
:causes
first
(ex-data))
:cljs (-> (gx/failures s)
:my-component
:internal-data
:ex
:causes
first
(ex-data))))))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
Expand Down
46 changes: 31 additions & 15 deletions test/k16/gx/beta/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
(let [graph (load-config)
gx-map (gx/normalize {:context context
:graph graph})]
(gx.schema/validate-graph gx-map))
)
(gx.schema/validate-graph gx-map)))

(deftest graph-tests
(let [graph (load-config)
Expand Down Expand Up @@ -205,7 +204,7 @@
'(gx/ref :http/server) {:port 8080}

'(gx/ref-keys [:http/server :db/url]) {:http/server {:port 8080}
:db/url "jdbc://foo/bar/baz"})))
:db/url "jdbc://foo/bar/baz"})))

#?(:cljs
(deftest globaly-registered-components-test
Expand Down Expand Up @@ -276,8 +275,7 @@
:node-key :b,
:node-contents #:gx{:start '(/ (gx/ref :z) 0),
:stop '(println "stopping")},
:signal-key :gx/start
:causes []}
:signal-key :gx/start}
{:internal-data
{:ex-message
(str "class clojure.lang.Keyword cannot be cast"
Expand All @@ -290,13 +288,13 @@
:error-type :node-signal,
:node-key :c,
:node-contents '(inc :bar),
:signal-key :gx/start
:causes []})
:signal-key :gx/start})
p-gx-started (gx/signal gx-norm :gx/start)]
(is (= expect
(->> @p-gx-started
:failures
(map #(update % :internal-data dissoc :ex)))))))))
(map #(update % :internal-data dissoc :ex))
(map #(dissoc % :causes)))))))))

(def props-validation-component
{:gx/start {:gx/props '(gx/ref :a)
Expand Down Expand Up @@ -333,8 +331,7 @@
:d '(gx/ref :c)}
gx-map {:graph graph
:context gx/default-context}]
(gx/normalize gx-map))
)
(gx/normalize gx-map)))

#?(:clj
(deftest dependency-node-failures-test
Expand Down Expand Up @@ -365,12 +362,12 @@
:error-type :node-signal,
:node-key :b,
:node-contents '(/ (gx/ref :a) 0),
:signal-key :gx/start
:causes []})
:signal-key :gx/start})
p-gx-started (gx/signal gx-map :gx/start)
failures (-> (:failures @p-gx-started)
(vec)
(update-in [2 :internal-data] dissoc :ex))]
(update-in [2 :internal-data] dissoc :ex)
(update 2 dissoc :causes))]
(is (= expect failures)))))

#?(:cljs
Expand Down Expand Up @@ -430,7 +427,7 @@
#:gx{:component 'k16.gx.beta.core-test/non-existent}
:internal-data
{:component 'k16.gx.beta.core-test/non-existent}
:causes [{:title :symbol-cannot-be-resolved
:causes [{:title :symbol-cannot-be-resolved
:data 'k16.gx.beta.core-test/non-existent}]}
(first (:failures gx-map)))))

Expand Down Expand Up @@ -569,7 +566,7 @@
failure (first (:failures norm))
cause (first (:causes failure))]
#?(:clj
(is (instance? java.io.FileNotFoundException (:exception cause))))
(is (instance? java.io.FileNotFoundException (:exception cause))))
(is (= :symbol-cannot-be-resolved (:title cause)))
(is (= {:message "Unable to resolve symbol",
:error-type :normalize-node,
Expand Down Expand Up @@ -622,3 +619,22 @@
(is (= {:my-comp {}} (gx/values started)))
(is (= {} (-> stopped :graph :my-comp :gx/props)))))))

(def ^:export incorrect-schema-component
{:gx/start {:gx/processor (fn start [_] :started)
:gx/props-schema [:map
[:foo {:optional true}
[:bar :string]]]}
:gx/stop {:gx/processor (fn stop [_] :stopped)}})

(deftest incorrect-schema-errors-test
(let [graph {:config {:foo {:bar "something"}}
:comp {:gx/component 'k16.gx.beta.core-test/incorrect-schema-component
:gx/props '(gx/ref :config)}}]
(test-async
(gx/signal {:graph graph} :gx/start)
(fn [started]
(let [{:keys [error-type causes] :as failures} (:comp (gx/failures started))]
(is (= :props-validation error-type))
(is (= ":malli.core/invalid-schema {:schema :bar}"
(-> causes first ex-message))))))))

1 comment on commit 5d79cb4

@vercel
Copy link

@vercel vercel bot commented on 5d79cb4 Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

gx – ./

gx-kepler16.vercel.app
gx.kepler16.com
gx-git-master-kepler16.vercel.app

Please sign in to comment.