Skip to content

Commit c784483

Browse files
committed
Implement configurable exception handling
1 parent aa05752 commit c784483

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change Log
22

3+
* Release 1.0.next in progress
4+
* Make exception handling in shallow conversions configurable: an `:exceptions` option may be `:group`, `:omit`, `:quaify`, or `:return`.
5+
* Fix bug in shallow array handling.
6+
37
* Release 1.0.73 on 2020-05-31
48
* Add `from-java-shallow` to provide functionality similar to `clojure.core/bean` (a shallow conversion) but with options to control behavior (so "dangerous" methods that appear as getters can be omitted).
59
* Bump `org.clojure/tools.logging` to `1.1.0`.

src/main/clojure/clojure/java/data.clj

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
conversion:
4545
* :add-class -- if true, add :class with the actual class of the object
4646
being converted -- this mimics clojure.core/bean.
47+
* :exceptions -- controls how getter exceptions should be handled:
48+
* :group -- return an :exceptions hash map in the object that
49+
contains all the properties that failed, with their exceptions,
50+
* :omit -- ignore exceptions and omit the properties that caused them,
51+
* :qualify -- return the exception as :<property>/exception and
52+
omit the property itself,
53+
* :return -- simply return the exception as the value of the property.
4754
* :omit -- a set of properties (keywords) to omit from the conversion
4855
so that unsafe methods are not called."
4956
(fn [obj _] (class obj)))
@@ -263,11 +270,28 @@
263270
(let [clazz (.getClass instance)]
264271
(if (.isArray clazz)
265272
((:from-shallow (add-array-methods clazz)) instance opts)
266-
(let [getter-map (reduce add-shallow-getter-fn {} (get-property-descriptors clazz))]
267-
(into (if (:add-class opts) {:class (class instance)} {})
268-
(for [[key getter-fn] (seq getter-map)
269-
:when (not (contains? (:omit opts) key))]
270-
[key (getter-fn instance)]))))))
273+
(let [getter-map (reduce add-shallow-getter-fn {} (get-property-descriptors clazz))
274+
exs (atom [])
275+
pairs (for [[key getter-fn] (seq getter-map)
276+
:when (not (contains? (:omit opts) key))
277+
:let [[k v]
278+
(if-let [exh (:exceptions opts)]
279+
(try
280+
[key (getter-fn instance)]
281+
(catch Throwable t
282+
(case exh
283+
:group (swap! exs conj [key t])
284+
:omit nil
285+
:qualify [(keyword (name key)
286+
"exception") t]
287+
:return [key t])))
288+
[key (getter-fn instance)])]
289+
:when k]
290+
[k v])]
291+
(cond-> {}
292+
(:add-class opts) (assoc :class (class instance))
293+
(seq @exs) (assoc :exceptions (into {} @exs))
294+
(seq pairs) (into pairs))))))
271295

272296
(doseq [clazz [String Character Byte Short Integer Long Float Double
273297
java.math.BigInteger java.math.BigDecimal]]

0 commit comments

Comments
 (0)