replace throw-illegal with ex-info

This commit is contained in:
Nathan Marz 2020-09-17 15:36:16 -04:00
parent 083376f904
commit efaf35558a
5 changed files with 24 additions and 35 deletions

View file

@ -1308,7 +1308,7 @@
ns (namespace structure)]
(cond (keyword? structure) (keyword ns new-name)
(symbol? structure) (symbol ns new-name)
:else (i/throw-illegal "NAME can only be used on symbols or keywords - " structure)
:else (throw (ex-info "NAME can only be used on symbols or keywords" {:structure structure}))
))))
(defnav ^{:doc "Navigates to the namespace portion of the keyword or symbol"}
@ -1321,7 +1321,8 @@
new-ns (next-fn (namespace structure))]
(cond (keyword? structure) (keyword new-ns name)
(symbol? structure) (symbol new-ns name)
:else (i/throw-illegal "NAMESPACE can only be used on symbols or keywords - " structure)
:else (throw (ex-info "NAMESPACE can only be used on symbols or keywords"
{:structure structure}))
))))
(defdynamicnav

View file

@ -2,7 +2,7 @@
#?(:cljs (:require-macros
[com.rpl.specter.util-macros
:refer [doseqres mk-comp-navs mk-late-fn mk-late-fn-records]]
[com.rpl.specter.impl :refer [throw-illegal]]))
))
;; workaround for cljs bug that emits warnings for vars named the same as a
;; private var in cljs.core (in this case `NONE`, added as private var to
;; cljs.core with 1.9.562)
@ -50,24 +50,6 @@
([a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 & r] v)))
#?(:clj
(defmacro throw* [etype & args]
`(throw (new ~etype (smart-str ~@args)))))
#?(
:clj
(defmacro throw-illegal [& args]
(let [platform (if (contains? &env :locals) :cljs :clj)]
(if (= platform :clj)
`(throw* IllegalArgumentException ~@args)
`(com.rpl.specter.impl/throw-illegal* ~@args)
)))
:cljs
(defn throw-illegal* [& args]
(throw (js/Error. (apply str args)))))
;; need to get the expansion function like this so that
;; this code compiles in a clojure environment where cljs.analyzer
;; namespace does not exist
@ -89,7 +71,7 @@
:cljs
(defn clj-macroexpand-all [form]
(throw-illegal "not implemented")))
(throw (ex-info "not implemented" {}))))
#?(
@ -98,7 +80,7 @@
:cljs
(defn intern* [ns name val]
(throw-illegal "intern not supported in ClojureScript")))
(throw (ex-info "intern not supported in ClojureScript" {}))))
#?(
:clj
@ -152,7 +134,9 @@
(defn- coerce-object [this]
(cond (rich-nav? this) this
(satisfies? p/ImplicitNav this) (p/implicit-nav this)
:else (throw-illegal "Not a navigator: " this " " (pr-str (type this)))))
:else (throw (ex-info "Not a navigator"
{:this this
:type-str (pr-str (type this))}))))
(defprotocol CoercePath
@ -376,7 +360,8 @@
(let [curr (get-cell res)]
(if (identical? curr NONE)
(set-cell! res structure)
(throw-illegal "More than one element found in structure: " structure))))]
(throw (ex-info "More than one element found in structure"
{:structure structure})))))]
(compiled-traverse* path result-fn structure)
(let [ret (get-cell res)]
@ -391,11 +376,12 @@
(let [curr (get-cell res)]
(if (identical? curr NONE)
(set-cell! res structure)
(throw-illegal "More than one element found in structure: " structure))))]
(throw (ex-info "More than one element found in structure"
{:structure structure})))))]
(compiled-traverse* path result-fn structure)
(let [ret (get-cell res)]
(if (identical? NONE ret)
(throw-illegal "Found no elements for select-one! on " structure))
(throw (ex-info "Found no elements for select-one!" {:structure structure})))
ret)))
@ -813,7 +799,8 @@
(defn dynamic-val-code [code possible-params]
(let [[i] (keep-indexed (fn [i v] (if (= v code) i)) possible-params)]
(if (nil? i)
(throw-illegal "Could not find " code " in possible params " possible-params))
(throw (ex-info "Could not find code in possible params"
{:code code :possible-params possible-params})))
(maybe-direct-nav
(->LocalParam i)
(direct-nav? code)))))
@ -985,9 +972,9 @@
(defn- multi-transform-error-fn [& nav]
(throw-illegal
"All navigation in multi-transform must end in 'terminal' "
"navigators. Instead navigated to: " nav))
(throw
(ex-info "All navigation in multi-transform must end in 'terminal' navigators"
{:nav nav})))
(defn compiled-multi-transform* [path structure]
(compiled-transform* path multi-transform-error-fn structure))

View file

@ -6,8 +6,8 @@
(defn- determine-params-impls [impls]
(let [grouped (->> impls (map (fn [[n & body]] [n body])) (into {}))]
(if-not (= #{'select* 'transform*} (-> grouped keys set))
(i/throw-illegal "defnav must implement select* and transform*, instead got "
(keys grouped)))
(throw (ex-info "defnav must implement select* and transform*"
{:methods (keys grouped)})))
grouped))

View file

@ -705,7 +705,8 @@
(insert-before-idx [_ idx val]
(if (= 0 idx)
(list val)
(i/throw-illegal "For a nil structure, can only insert before index 0, not at - " idx)))
(throw (ex-info "For a nil structure, can only insert before index 0"
{:insertion-index idx}))))
#?(:clj java.lang.String :cljs string)
(insert-before-idx [aseq idx val]

View file

@ -65,4 +65,4 @@
`(defn ~'late-fn [~f ~args]
(case (count ~args)
~@(apply concat cases)
(com.rpl.specter.impl/throw-illegal "Cannot have late function with more than 18 args")))))
(throw (ex-info "Cannot have late function with more than 18 args" {}))))))