Skip to content

Commit 2b68b42

Browse files
Report invalid enum->fn key in exception message (#3)
* report invalid key in exception message too * test invalid key is reported on the exception message
1 parent d44ca3a commit 2b68b42

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/clj/qbits/commons/enum.clj

+6-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
(str enum-symbol "/")
7373
symbol)])
7474
enum-map)
75-
(throw (ex-info ~(format "Invalid Enum key - possible keys are -> %s"
76-
(str/join ", " (map key enum-map)))
77-
{:type ::invalid-enum-value
78-
:key x#})))))))
75+
(throw (ex-info
76+
(format "Invalid Enum key - %s - possible keys are -> %s"
77+
x#
78+
~(str/join ", " (map key enum-map)))
79+
{:type ::invalid-enum-value
80+
:key x#})))))))

test/qbits/commons/enum_test.clj

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns qbits.commons.enum-test
22
(:require
33
[clojure.test :as t :refer [deftest testing is]]
4-
[qbits.commons.enum :as sut])
4+
[qbits.commons.enum :as sut]
5+
[clojure.string :as str])
56
(:import
67
[java.math RoundingMode]))
78

@@ -12,14 +13,16 @@
1213
(is (instance? RoundingMode v))
1314
(is (= "UP" (str v)))))
1415

15-
(testing "invalid key is reported in ex-data"
16+
(testing "invalid key is reported in ex-data and exception message"
1617
(let [efn (sut/enum->fn RoundingMode)
1718
v (try
1819
(efn :foo)
1920
(catch Exception err err))
2021
{exd-t :type
2122
exd-k :key
22-
:as exd} (ex-data v)]
23+
:as exd} (ex-data v)
24+
ex-msg (when (instance? Exception v) (.getMessage ^Exception v))]
2325
(is (some? exd))
2426
(is (= ::sut/invalid-enum-value exd-t))
25-
(is (= :foo exd-k)))))
27+
(is (= :foo exd-k))
28+
(is (str/starts-with? ex-msg "Invalid Enum key - :foo" )))))

0 commit comments

Comments
 (0)