Skip to content

Commit

Permalink
Merge pull request #530 from fluree/feature/humanized-error-messages
Browse files Browse the repository at this point in the history
DRY up query validation error humanization
  • Loading branch information
cap10morgan authored Jul 14, 2023
2 parents a1f0e7b + 1892c94 commit faebe66
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/fluree/db/api/query.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns fluree.db.api.query
"Primary API ns for any user-invoked actions. Wrapped by language & use specific APIS
that are directly exposed"
(:require [clojure.core.async :as async :refer [<! go]]
(:require [clojure.core.async :as async]
[fluree.db.fuel :as fuel]
[fluree.db.time-travel :as time-travel]
[fluree.db.query.fql :as fql]
Expand All @@ -11,7 +11,8 @@
[fluree.db.util.core :as util :refer [try* catch*]]
[fluree.db.util.async :as async-util :refer [<? go-try]]
[fluree.db.json-ld.policy :as perm]
[fluree.db.json-ld.credential :as cred]))
[fluree.db.json-ld.credential :as cred]
[fluree.db.validation :as v]))

#?(:clj (set! *warn-on-reflection* true))

Expand Down Expand Up @@ -89,7 +90,7 @@
(str "History query not properly formatted. Provided "
(pr-str query-map))
{:status 400
:message (history/humanize-error e)
:message (v/humanize-error e)
:error :db/invalid-query}))))
history-query (cond-> coerced-query did (assoc-in [:opts :did] did))]
(<? (history* db history-query)))))
Expand Down Expand Up @@ -148,7 +149,7 @@
[source flureeQL]
(go-try
(let [{flureeQL :subject, did :did} (or (<? (cred/verify flureeQL))
{:subject flureeQL})
{:subject flureeQL})
global-opts (cond-> (:opts flureeQL) did (assoc :did did))
db (if-let [policy-opts (perm/policy-opts global-opts)]
(<? (perm/wrap-policy source policy-opts))
Expand Down
8 changes: 4 additions & 4 deletions src/fluree/db/query/exec/eval.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@
;; hash fns
sha256 sha512
;; rdf term fns
uuid struuid isNumeric isBlank str
})
uuid struuid isNumeric isBlank str})


(def allowed-symbols
(set/union allowed-aggregate-fns allowed-scalar-fns))
Expand Down Expand Up @@ -307,8 +307,8 @@
struuid fluree.db.query.exec.eval/struuid
isNumeric fluree.db.query.exec.eval/isNumeric
isBlank fluree.db.query.exec.eval/isBlank
str fluree.db.query.exec.eval/sparql-str
})
str fluree.db.query.exec.eval/sparql-str})


(defn variable?
[sym]
Expand Down
4 changes: 2 additions & 2 deletions src/fluree/db/query/fql/syntax.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
(throw (ex-info "Invalid Query"
{:status 400
:error :db/invalid-query
:reasons (-> e ex-data :data :explain me/humanize)})))))
:reasons (v/humanize-error e)})))))

(def coerce-modification*
(m/coercer ::modification (mt/transformer {:name :fql}) {:registry registry}))
Expand All @@ -180,4 +180,4 @@
(throw (ex-info "Invalid Ledger Modification"
{:status 400
:error :db/invalid-query
:reasons (-> e ex-data :data :explain me/humanize)})))))
:reasons (v/humanize-error e)})))))
4 changes: 0 additions & 4 deletions src/fluree/db/query/history.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@
(def explain-error
(m/explainer ::history-query {:registry registry}))

(defn humanize-error
[query-validation-error]
(-> query-validation-error Throwable->map :data :data :explain me/humanize))

(def parse-history-query
(m/parser ::history-query {:registry registry}))

Expand Down
7 changes: 6 additions & 1 deletion src/fluree/db/validation.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns fluree.db.validation
(:require [fluree.db.util.core :refer [pred-ident?]]
[fluree.db.constants :as const]
[malli.core :as m]))
[malli.core :as m]
[malli.error :as me]))

(defn iri?
[v]
Expand Down Expand Up @@ -54,6 +55,10 @@
[x]
(or (fn-string? x) (fn-list? x)))

(defn humanize-error
[error]
(-> error ex-data :data :explain me/humanize))

(def registry
(merge
(m/base-schemas)
Expand Down

0 comments on commit faebe66

Please sign in to comment.