Skip to content

Commit

Permalink
global ex-handler enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
robertluo committed Jun 12, 2020
1 parent 5e282a3 commit 5508c43
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ If a value is not present in data, its value pulled will be `:robertluo.pullable

If a value is a function, you can pass `:with` arguments, it will apply these arguments to the function and return it.

### `:batch` option

If you want to call `:with` multiple times, `:batch` option will return a sequence of calling returning value also in a sequence.

## Error Handling

When an exception raised when pulling a key, the corresponding value will be an error map like:

`{:error/key :key-of-error, :error/message "the exception message", :error/data {...}}`

You can provide an global exception handler function which will receive this error map as the argument, and the return value will be the value instead. A common practice might be logging.


## License
Copyright © 2020 Robertluo

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
10 changes: 7 additions & 3 deletions src/robertluo/pullable.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
`:seq` option also can specific offset and limit. e.g. `:seq [10 20]` means pulling
from the collection starting from index 10 (0 based), and take 20.
"
[data ptn]
(when-let [q (some-> (ptn/pattern->query ptn) core/query)]
(core/-select q data)))
([data ptn]
(pull data ptn nil))
([data ptn ex-handler]
(when-let [q (some-> (ptn/pattern->query ptn)
(assoc :ex-handler ex-handler)
core/query)]
(core/-select q data))))
9 changes: 5 additions & 4 deletions src/robertluo/pullable/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@
[::batch (BatchProcessor. calls)])

(defn query
[qspec]
[{:keys [options children ex-handler] :as qspec}]
(map->CoreQuery (assoc qspec
:children (->> (:children qspec)
(map query))
:processors (->> (:options qspec)
:children (cond->> children
ex-handler (map #(assoc % :ex-handler ex-handler))
true (map query))
:processors (->> options
(cons [::core (CoreProcessor.)])
(map option-create)
(into (array-map))
Expand Down
5 changes: 4 additions & 1 deletion test/robertluo/pullable_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@
(sut/pull data '(:fn2 :with [7])))))
(testing ":batch option will batch :with calls"
(is (= {:fn2 [{:val 8} {:val 9}]}
(sut/pull data '(:fn2 :batch [[7] [8]])))))))
(sut/pull data '(:fn2 :batch [[7] [8]])))))
(testing "global error handler option allow you to handle exceptions"
(is (= {:fn2 :fn2}
(sut/pull data '(:fn2 :with ["ok"]) :error/key))))))

0 comments on commit 5508c43

Please sign in to comment.