Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra error message context for fn executions #791

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/clj/fluree/db/query/exec/eval.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
allowed-scalar-fns)]
(if (contains? allowed-fns sym)
(let [qsym (get qualified-symbols sym sym)]
(log/debug "qualified symbol" sym "as" qsym)
(log/trace "qualified symbol" sym "as" qsym)
bplatz marked this conversation as resolved.
Show resolved Hide resolved
qsym)
(let [err-msg (if (and (not allow-aggregates?)
(contains? allowed-aggregate-fns sym))
Expand Down Expand Up @@ -442,7 +442,7 @@
(log/trace "fn bindings:" (quote ~bdg))
(let ~bdg
~qualified-code))]
(log/debug "compiled fn:" fn-code)
(log/trace "compiled fn:" fn-code)
(eval fn-code))))

(defn compile-filter
Expand Down
30 changes: 23 additions & 7 deletions src/clj/fluree/db/query/exec/where.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns fluree.db.query.exec.where
(:require [fluree.db.query.range :as query-range]
[clojure.core.async :as async :refer [>! go]]
[clojure.string :as str]
[fluree.db.flake :as flake]
[fluree.db.fuel :as fuel]
[fluree.db.index :as index]
Expand Down Expand Up @@ -425,16 +426,31 @@
(-match-class active-graph fuel-tracker solution triple error-ch)))
nil-channel))

(defn filter-exception
"Reformats raw filter exception to try to provide more useful feedback."
[e solution f]
(let [fn-str (->> f meta :fns (str/join " "))
ex-msg (or (ex-message e)
;; note: NullPointerException is common but has no ex-message, create one
(let [ex-type (str (type e))] ;; attempt to make JS compatible
(if (= ex-type "class java.lang.NullPointerException")
"Variable has null value, cannot apply filter"
"Unknown error")))
e* (ex-info (str "Exception in statement '[filter " fn-str "]': " ex-msg)
{:status 400
:error :db/invalid-query}
e)]
(log/warn (ex-message e*))
e*))

(defmethod match-pattern :filter
[_ds _fuel-tracker solution pattern error-ch]
(go
(try*
(let [f (pattern-data pattern)]
(when (f solution)
solution))
(catch* e
(log/error e "Error applying filter")
(>! error-ch e)))))
(let [f (pattern-data pattern)]
(try*
(when (f solution)
solution)
(catch* e (>! error-ch (filter-exception e solution f)))))))

(defn with-constraint
"Return a channel of all solutions from the data set `ds` that extend from the
Expand Down
2 changes: 1 addition & 1 deletion src/clj/fluree/db/query/fql/parse.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@
(map parse-code)
(map eval/compile)
(apply every-pred))]
[(where/->pattern :filter f)]))
[(where/->pattern :filter (with-meta f {:fns codes}))]))

(defmethod parse-pattern :union
[[_ & unions] vars context]
Expand Down
18 changes: 14 additions & 4 deletions src/clj/fluree/db/query/range.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@
[f]
(= f ::unauthorized))

(defn authorize-flake-exception
"Wraps upstream exception and logs out a warning message"
[e db flake]
(let [e* (ex-info (str "Policy exception authorizing Flake in "
(:alias db) "?t=" (:t db)
". " (ex-message e))
{:error :db/policy-exception
:status 400
:flake flake}
e)]
(log/warn (ex-message e*))
e*))

(defn authorize-flake
[db error-ch flake]
(go
Expand All @@ -103,10 +116,7 @@
flake
::unauthorized)
(catch* e
(log/error e
"Error authorizing flake in ledger"
(select-keys db [:network :ledger-id :t]))
(>! error-ch e)))))
(>! error-ch (authorize-flake-exception e db flake))))))

(defn authorize-flakes
"Authorize each flake in the supplied `flakes` collection asynchronously,
Expand Down
Loading