This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-05
Channels
- # aleph (10)
- # announcements (2)
- # babashka (83)
- # beginners (41)
- # biff (2)
- # calva (6)
- # cider (7)
- # clerk (3)
- # clj-kondo (7)
- # clojure (89)
- # clojure-berlin (1)
- # clojure-brasil (4)
- # clojure-dev (15)
- # clojure-europe (16)
- # clojure-nl (1)
- # clojure-norway (54)
- # clojure-spec (1)
- # clojure-uk (4)
- # clojurescript (2)
- # conjure (3)
- # datomic (7)
- # emacs (4)
- # events (1)
- # figwheel-main (1)
- # graalvm (5)
- # gratitude (2)
- # honeysql (14)
- # hyperfiddle (86)
- # introduce-yourself (1)
- # joyride (9)
- # lsp (179)
- # malli (29)
- # music (1)
- # nyc (14)
- # off-topic (6)
- # polylith (16)
- # rdf (8)
- # releases (2)
- # rum (1)
- # shadow-cljs (26)
- # specter (2)
- # sql (4)
- # uncomplicate (2)
- # xtdb (16)
Is there a way to add a generator to an existing :malli.core/schema
object stored in a var in another namespace?
You could alter-var-root to the var, and use u/update-properties to assoc in the generator key of your choice
Is there a way to get pretty printed and concise error messages from failed (reitit) coercion and instrumentation? I have instrumented db queries and coerced api routes that return large amounts of data and it鈥檚 difficult to see what the exact offending data is from the exception. If it鈥檚 only 1 record it would be great to see only that record, and if it's hundreds, it would be nice to only see a few of them
Did you try
already? https://cljdoc.org/d/metosin/reitit/0.7.0-alpha3/doc/basics/error-messages#pretty-errors
Thanks @U6N4HSMFW , I will look at that! @U051GFP2V , I was also wondering if one can get more legible exceptions from instrumented functions (not reitit-related)
There鈥檚 similar pretty reporter for malli. I don鈥檛 know how it reports instrumented functions but probably quickest way to find out is to try 馃槈
Oh, I know how that works actually, @UGDTSFM4M.
For instrumentation?
what you want to do is plug in your :report function when you call instrument!
(or -instument
)
which is done here: https://github.com/metabase/metabase/blob/master/src/metabase/util/malli.cljc#L69
and then, you may want to call me/humanize
, and plug in some kind of wrap function for it
Oh, wow. This looks amazing. I need to wrap my head around how this can be pieced together but shouldn鈥檛 be that hard.
I could use it too, I have a couple of :sequential schemes that return thousands of complex maps and overwhelm Emacs. Isn't there already a similar reporting function? Not to reinvent the wheel 馃槈
I am working on a solution for instrumented functions. Happy to show my solution next week and get feedback!
So this is what I managed to achieve. For coercion errors, I added the following custom exception handler to my reitit routes
{:reitit.coercion/response-coercion
(fn [exception request]
(log/error "Response coercion error"
(with-out-str
(pprint/pprint
(malli-error/humanize
(ex-data exception)
:request))))
{:status 500
:body {:message "Response coercion error"
:uri (:uri request)}})
,,,}
This logs the humanized coercion error. Although it prints all failing records in a large dataset, they will probably all follow the same pattern and it's pretty easy to spot what the actual error is.
For instrumentation I added this function as my :report
option to instrument!
(defn instrument-error!
[type data]
(let [{:keys [input args output value arity]} data
[cause humanized] (cond
input ["Invalid input" (malli-error/humanize (m/explain input args))]
output ["Invalid output" (malli-error/humanize (m/explain output value))]
arity ["Invalid arity" "Too few or too many args"])]
(throw (ex-info
(str cause "\n" (with-out-str (pprint/pprint humanized)))
{:type type :data data}))))
(I added arity errors for completeness but I guess that Clojure will catch arity errors)
When developing in the REPL, I get the instrumentation error printed as the exception message, which is handy. I also added this to my reitit custom exception handling
(defn instrument-error
[exception request]
(log/error (ex-message exception))
{:status 500
:body {:message "Application error"
:uri (:uri request)}})
;; Custom exception handling map
{,,,
::m/invalid-output instrument-error
::m/invalid-input instrument-error
,,,}
So I get instrumentation errors in a request logged.
I am sure there are better ways to do this so any feedback is welcome. Also, it would be great if the instrumentation error message could include the name of the failing function. I can't locate it in the data received by the :report
function but I guess that could be extracted from the stack trace? I have no idea how to do that though.Any idea on how to get the name of the instrumented function that failed?
So something like this for example:
(defn instrument-error!
[type data]
(let [{:keys [input args output value arity]} data
[cause humanized] (cond
input ["Invalid input" (malli-error/humanize (m/explain input args))]
output ["Invalid output" (malli-error/humanize (m/explain output value))]
arity ["Invalid arity" "Too few or too many args"])]
(throw (ex-info
(str cause " in " (:fn-name data) "\n" (with-out-str (pprint/pprint humanized)))
{:type type :data data}))))