-
Notifications
You must be signed in to change notification settings - Fork 204
/
pretty.cljc
74 lines (63 loc) · 2.53 KB
/
pretty.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(ns malli.dev.pretty
(:require [malli.dev.virhe :as v]
[malli.error :as me]
[malli.core :as m]))
(defn -printer
([] (-printer nil))
([options]
(v/-printer
(merge
{:title "Schema Error"
:width 100
:colors v/-dark-colors
:unknown (fn [x] (when (m/schema? x) (m/form x)))
:throwing-fn-top-level-ns-names ["malli" "clojure"]}
options))))
(defn -errors [schema value printer]
(->> (for [error (->> value (m/explain schema) (me/with-error-messages) :errors)]
(v/-visit (into {} error) printer))
(interpose :break)))
(defn -block [text body printer]
[:group (v/-text text printer) :break :break [:align 2 body]])
(defn -link [link printer]
(v/-color :link link printer))
;;
;; formatters
;;
(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:" (-link "https://cljdoc.org/d/metosin/malli/CURRENT/doc/function-schemas" printer) printer)]})
(defmethod v/-format ::m/invalid-output [_ _ {:keys [value output]} printer]
{:body
[:group
(-block "Invalid function return value:" (v/-visit value printer) printer) :break :break
(-block "Output Schema:" (v/-visit output printer) printer) :break :break
(-block "Errors:" (-errors output value printer) printer) :break :break
(-block "More information:" (-link "https://cljdoc.org/d/metosin/malli/CURRENT/doc/function-schemas" printer) printer)]})
(defmethod v/-format ::m/invalid-arity [_ _ {:keys [args arity schema]} printer]
{:body
[:group
(-block (str "Invalid function arity (" arity "):") (v/-visit args printer) printer) :break :break
(-block "Function Schema:" (v/-visit schema printer) printer) :break :break
(-block "More information:" (-link "https://cljdoc.org/d/metosin/malli/CURRENT/doc/function-schemas" printer) printer)]})
;;
;; public api
;;
(defn reporter
([] (reporter (-printer)))
([printer]
(fn [type data]
(-> (ex-info (str type) {:type type :data data})
(v/-exception-doc printer)
(v/-print-doc printer)))))
(defn thrower
([] (thrower (-printer)))
([printer]
(let [report (reporter printer)]
(fn [type data]
(let [message (with-out-str (report type data))]
(throw (ex-info message {:type type :data data})))))))