This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-02
Channels
- # announcements (3)
- # aws (2)
- # babashka (60)
- # beginners (21)
- # cljs-dev (35)
- # cljsrn (3)
- # clojure (53)
- # clojure-android (2)
- # clojure-australia (3)
- # clojure-europe (45)
- # clojure-france (4)
- # clojure-nl (4)
- # clojure-uk (6)
- # clojurescript (33)
- # core-typed (1)
- # cursive (13)
- # datomic (6)
- # duct (1)
- # emacs (2)
- # fulcro (10)
- # introduce-yourself (3)
- # jobs (2)
- # jobs-discuss (13)
- # leiningen (1)
- # malli (19)
- # missionary (63)
- # music (1)
- # off-topic (21)
- # pathom (3)
- # polylith (18)
- # practicalli (12)
- # proletarian (1)
- # reagent (40)
- # reitit (23)
- # releases (1)
- # remote-jobs (1)
- # ring (14)
- # ring-swagger (1)
- # shadow-cljs (13)
- # sql (30)
- # testing (27)
- # tools-deps (31)
- # vim (10)
- # xtdb (4)
malli.dev
is, the red underline & bump are from kondo. The pretty printer is currently just for runtime errors.
the error formatters use fipp, look like this:
(defmethod v/-format ::m/invalid-input [_ _ {:keys [args input]} printer]
{:body
[:group
(-block "Invalid function arguments:" (v/-visit args printer) printer) :break :break
(-block "Input Schema:" (v/-visit input printer) printer) :break :break
(-block "Errors:" (-errors input args printer) printer) :break :break
(-block "More information:" (v/-color :link "" printer) printer)]})
there will be pretty/reporter
to just prints the errors and pretty/thrower
to throw ’em.
that looks great
is there a better idiom for this?
(defn Step? [step]
(if (m/validate Step step)
step
(throw (Exception.
(->> step
(m/explain Step)
(me/humanize))))))
haha I literally wrote that exact function 5 mins ago and was wondering the same thing
• you could create a validator & explainer before, if perf matters
• I recommend throwing ex-imfo
with :type
, easier to catalog the errors
• humanize can fail in some corner cases, should be robust, fixes welcome
• there could be a helper for this in the future in malli, using m/-fail!
which works well with the upcoming pretty printer
Malli internal errors just throw the minimum set of data (to be fast), to be post-processed when rendering the error. e.g. explain
and humanize
are pure, can be done on rendering, if the value & schema are present at ex-data
. Might not be relevant on actual projects.
I haven't heard of a robust way to override the repl default exception handler (from the repl) - would allow pretty errors for all Clojure: just throw data and add custom renderers for all :type
s & exception classes
so something like this?
(defn SimpleStep? [step]
(if (validate-step @step)
step
(throw (ex-info "Step isn't valid" {:type (explain-step @step)}))))
good point about the validator and explainer, thanks
I'm leaning towards this form:
(defn needs-a-good-name
[s]
(let [s (m/schema s)
v (m/validator s)
d (m/decoder s ,,,) ;; optional?
e (m/explainer s)]
(fn [x]
(let [x (d x)]
(if (v x)
x
(throw (ex-info "invalid something" (e x))))))))
More generally, you can pass two functions, on-success and on-fail, then you can decide if you want to throw or drop or invalid data