Skip to content

Commit

Permalink
Improve wrapper process error
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Dec 13, 2024
1 parent c14aee7 commit d8955c9
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 37 deletions.
13 changes: 7 additions & 6 deletions src/main/com/wsscode/pathom3/interface/async/eql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
(>defn process-ast
[env ast]
[::pcra/env :edn-query-language.ast/node => p/promise?]
(-> (p/let [env env]
(p.plugin/run-with-plugins env ::p.eql/wrap-process-ast
process-ast* env ast))
(p/catch
(fn [e]
(throw (p.eql/process-error env ast e))))))
(let [source-entity (or (p.ent/entity env) {})]
(-> (p/let [env env]
(p.plugin/run-with-plugins env ::p.eql/wrap-process-ast
process-ast* env ast))
(p/catch
(fn [e]
(throw (p.eql/process-error env ast source-entity e)))))))

(>defn process
"Evaluate EQL expression using async runner.
Expand Down
26 changes: 16 additions & 10 deletions src/main/com/wsscode/pathom3/interface/eql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@
(str (subs s 0 (- max-size 3)) "...")
s))

(defn process-error [env ast e]
(let [entity (p.ent/entity env)]
(defn process-error [env ast source-entity error]
(let [entity (or (p.ent/entity env) {})
tx (eql/ast->query ast)]
(ex-info
(str "Error while processing request " (string-cap (pr-str (eql/ast->query ast)) 20) " for entity " (pr-str (or entity {})))
{:entity entity}
e)))
(str "Error while processing request "
(string-cap (pr-str tx) 40)
" for entity "
(string-cap (pr-str source-entity) 40))
{:entity entity
:tx tx}
error)))

(>defn process-ast
[env ast]
[(s/keys) :edn-query-language.ast/node => map?]
(try
(p.plugin/run-with-plugins env ::wrap-process-ast
process-ast* env ast)
(catch #?(:clj Throwable :cljs :default) e
(throw (process-error env ast e)))))
(let [source-entity (or (p.ent/entity env) {})]
(try
(p.plugin/run-with-plugins env ::wrap-process-ast
process-ast* env ast)
(catch #?(:clj Throwable :cljs :default) e
(throw (process-error env ast source-entity e))))))

(>defn process
"Evaluate EQL expression.
Expand Down
35 changes: 19 additions & 16 deletions test/com/wsscode/pathom3/interface/async/eql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[com.wsscode.pathom3.connect.built-in.resolvers :as pbir]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.connect.operation :as pco]
[com.wsscode.pathom3.connect.planner :as pcp]
[com.wsscode.pathom3.connect.runner :as pcr]
[com.wsscode.pathom3.interface.async.eql :as p.a.eql]
[com.wsscode.pathom3.test.geometry-resolvers :as geo]
[promesa.core :as p]))
Expand Down Expand Up @@ -69,7 +71,7 @@
[(pbir/constantly-resolver :a 10)])
{:pathom/eql [:a]})
meta
:com.wsscode.pathom3.connect.runner/run-stats))))
::pcr/run-stats))))

(testing "include when requested"
(is (some?
Expand All @@ -79,7 +81,7 @@
{:pathom/eql [:a]
:pathom/include-stats? true})
meta
:com.wsscode.pathom3.connect.runner/run-stats)))))
::pcr/run-stats)))))

(deftest process-one-test
(is (= @(p.a.eql/process-one (pci/register registry) {:left 10 :right 30} :width)
Expand All @@ -98,17 +100,18 @@
(is (= response [{:a 1}]))
(check
(meta response)
=> {:com.wsscode.pathom3.connect.runner/run-stats
{:com.wsscode.pathom3.connect.planner/source-ast {},
:com.wsscode.pathom3.connect.planner/index-attrs {},
:com.wsscode.pathom3.connect.planner/user-request-shape {},
:com.wsscode.pathom3.connect.planner/root number?,
:com.wsscode.pathom3.connect.planner/available-data {},
:com.wsscode.pathom3.connect.runner/node-run-stats {},
:com.wsscode.pathom3.connect.planner/index-ast {},
:com.wsscode.pathom3.connect.runner/transient-stats {},
:com.wsscode.pathom3.connect.planner/index-resolver->nodes {},
:com.wsscode.pathom3.connect.planner/nodes {}}}))
=> {::pcr/run-stats
{::pcp/available-data {}
::pcp/index-ast {}
::pcp/index-attrs {}
::pcp/index-resolver->nodes {}
::pcp/nodes {}
::pcp/root number?
::pcp/source-ast {}
::pcp/user-request-shape {}

::pcr/node-run-stats {}
::pcr/transient-stats {}}}))

(testing "don't change data when its already there"
(let [response @(p.a.eql/process-one
Expand All @@ -119,8 +122,8 @@
(is (= response {:b 1}))
(check
(meta response)
=> {:com.wsscode.pathom3.connect.runner/run-stats
{:com.wsscode.pathom3.connect.planner/available-data
=> {::pcr/run-stats
{::pcp/available-data
{:a {}}}})))

(testing "returns false"
Expand All @@ -142,4 +145,4 @@
1e3)))
(testing
"uses same error message"
(is (= msg "clojure.lang.ExceptionInfo: Error while processing request [:a] for entity {} {:entity nil}")))))
(is (= msg "clojure.lang.ExceptionInfo: Error while processing request [:a] for entity {} {:entity {}, :tx [:a]}")))))
57 changes: 53 additions & 4 deletions test/com/wsscode/pathom3/interface/eql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[com.wsscode.pathom3.entity-tree :as p.ent]
[com.wsscode.pathom3.interface.eql :as p.eql]
[com.wsscode.pathom3.test.geometry-resolvers :as geo]
[com.wsscode.pathom3.test.helpers :as h]
[edn-query-language.core :as eql]))

(pco/defresolver coords []
Expand Down Expand Up @@ -95,15 +96,63 @@
{::coords #{{:right 35} {:right 25}}}))))

(deftest process-error-test
(is (thrown-with-msg?
#?(:clj Throwable :cljs js/Error)
#"Error while processing request \[:a] for entity \{}"
(check
(h/catch-exception
(p.eql/process
(pci/register
(pco/resolver 'a
{::pco/output [:a]}
(fn [_ _] {})))
[:a]))
=> {:ex/message "Error while processing request [:a] for entity {}"
:ex/data {:entity {}
:tx [:a]}})

(testing "caps request on ex message"
(check
(h/catch-exception
(p.eql/process
(pci/register
(pco/resolver 'a
{::pco/output [:a]}
(fn [_ _] {})))
[:having-a-query {:that-goes [:long]} :in-fact {:very [:long]}]))
=> {:ex/message "Error while processing request [:having-a-query {:that-goes [:long]}... for entity {}"
:ex/data {:entity {}
:tx [:having-a-query {:that-goes [:long]} :in-fact {:very [:long]}]}}))

(testing "caps entity on ex message"
(check
(h/catch-exception
(p.eql/process
(pci/register
(pco/resolver 'a
{::pco/output [:a]}
(fn [_ _] {})))
[:a]))))
{:some "Lorem Ipsum is simply dummy text of the printing and typesetting"}
[:a]))
=> {:ex/message "Error while processing request [:a] for entity {:some \"Lorem Ipsum is simply dummy t...",
:ex/data {:entity {:some "Lorem Ipsum is simply dummy text of the printing and typesetting"}
:tx [:a]}}))

(testing "ex data entity contains accumulated data while ex message shows the input entity"
(check
(h/catch-exception
(p.eql/process
(pci/register
[(pco/resolver 'b
{::pco/input [:a]
::pco/output [:b]}
(fn [_ _] {:b 2}))
(pco/resolver 'c
{::pco/input [:b]
::pco/output [:c]}
(fn [_ _] {}))])
{:a 1}
[:c]))
=> {:ex/message "Error while processing request [:c] for entity {:a 1}"
:ex/data {:entity {:a 1 :b 2}
:tx [:c]}})))

(deftest process-one-test
(is (= (p.eql/process-one (pci/register registry) {:left 10 :right 30} :width)
Expand Down
12 changes: 11 additions & 1 deletion test/com/wsscode/pathom3/test/helpers.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(ns com.wsscode.pathom3.test.helpers
(:require
[clojure.walk :as walk]))
[clojure.walk :as walk])
#?(:cljs
(:require-macros
[com.wsscode.pathom3.test.helpers])))

(defn spy [{:keys [return]}]
(let [calls (atom [])]
Expand Down Expand Up @@ -30,3 +33,10 @@
(assoc x ::meta (meta x))
x))
x))

(defmacro catch-exception [& body]
`(try
~@body
(catch #?(:clj Throwable :cljs :default) e#
{:ex/message (ex-message e#)
:ex/data (ex-data e#)})))

0 comments on commit d8955c9

Please sign in to comment.