-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
exception.cljs
305 lines (285 loc) · 10.2 KB
/
exception.cljs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
(ns portal.ui.viewer.exception
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[portal.colors :as c]
[portal.ui.filter :as-alias f]
[portal.ui.icons :as icon]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]
[portal.ui.viewer.log :as log]))
;;; :spec
(s/def ::cause string?)
(s/def ::trace-line
(s/cat :class (s/or :symbol symbol? :string string?)
:method (s/or :symbol symbol? :string string?)
:file (s/or :str string? :nil nil?)
:line number?))
(s/def ::trace (s/coll-of ::trace-line :min-count 1))
(s/def ::type (s/or :symbol symbol? :string string?))
(s/def ::message string?)
(s/def ::at ::trace-line)
(s/def ::via
(s/coll-of
(s/keys :req-un [::type]
:opt-un [::message ::at])))
(s/def ::exception
(s/keys :req-un [::via]
:opt-un [::trace ::cause]))
;;;
(defn trace? [value]
(s/valid? ::trace value))
(defn exception? [value]
(s/valid? ::exception value))
(defn- inspect-sub-trace [trace]
(let [trace (map meta trace)
theme (theme/use-theme)
context (ins/use-context)
{:keys [expanded? selected]} (ins/use-options)
background (ins/get-background)
{:keys [clj? sym class]} (first trace)
border (if selected
(get theme (nth theme/order selected))
(::c/border theme))
can-expand? (> (count trace) 1)
wrapper-options (ins/use-wrapper-options context)]
[:<>
[d/div
{:on-click (:on-click wrapper-options)
:on-double-click (:on-double-click wrapper-options)
:style {:grid-column "1"}}
[d/div
{:style
{:background background
:display :flex
:align-items :center
:position :relative
:cursor :pointer
:padding [(* 0.5 (:padding theme)) (:padding theme)]
:border-top-left-radius (:border-radius theme)
:border-bottom-left-radius (:border-radius theme)
:border-left [1 :solid border]
:border-top [1 :solid border]
:border-bottom [1 :solid border]}}
[d/div
{:style
{:width 1
:position :absolute
:top 0
:right -1
:bottom 0
:background background}}]
[d/div
{:style {:width "1em"}}
(when can-expand? [ins/toggle-expand])]
[theme/with-theme+
{::c/symbol (if clj?
(::c/namespace theme)
(::c/package theme))}
[select/with-position
{:row 0 :column 0}
[ins/inspector (symbol (if-not clj? class (namespace sym)))]]]]]
[d/div
{:on-click (:on-click wrapper-options)
:on-double-click (:on-double-click wrapper-options)
:style
{:grid-column "2"
:display :flex
:gap (:padding theme)
:background background
:padding [(* 0.5 (:padding theme)) (:padding theme)]
:border [1 :solid border]
:border-top-right-radius (:border-radius theme)
:border-bottom-right-radius (:border-radius theme)
:border-bottom-left-radius (when (and can-expand? expanded?) (:border-radius theme))}}
[d/div
{:style {:flex "1" :display :flex :flex-direction :column}}
(map-indexed
(fn [idx {:keys [clj? sym method index] :as source}]
[ins/with-key
index
[d/div
{:key idx
:style {:display :flex
:flex "1"
:justify-content :space-between}}
[d/div
[select/with-position
{:row idx :column 1}
[ins/inspector {:portal.viewer/default :portal.viewer/source-location}
(assoc source :label (if clj? (symbol nil (name sym)) method))]]]]])
(if expanded? trace (take 1 trace)))]
[d/div
{:style
{:width "2em" :text-align :right}}
(when can-expand?
[d/span
{:style
{:color (::c/border theme)}}
" [" (count trace) "]"])]]]))
(defn- analyze-trace-item [index trace]
(let [[class method file line] trace
class (cond-> class (string? class) symbol)
clj-name (demunge class)
clj? (or (and (string? file)
(str/ends-with? file ".clj"))
(not= clj-name class))]
(with-meta
trace
(merge
{:class class
:method method
:file file
:line line
:column 1
:index index}
(when-let [ns (and clj? (some-> clj-name namespace symbol))]
{:clj? true :ns ns :sym clj-name})))))
(defn- wrapper [context & children]
(let [opts (ins/use-options)
viewer (get-in opts [:viewer :name])]
(if (= viewer :portal.viewer/sub-trace)
(into [:<>] children)
[d/div
{:style {:grid-column "1 / 3"}}
(into [ins/wrapper context] children)])))
(defn- inspect-stack-trace [trace]
(let [theme (theme/use-theme)
options (ins/use-options)]
[d/div
{:style
{:border-top-left-radius (:border-radius theme)
:border-bottom-left-radius (:border-radius theme)
:border-left [5 :solid (::c/exception theme)]}}
[d/div
{:style
{:background (ins/get-background)
:box-sizing :border-box
:padding (:padding theme)
:display :grid
:grid-template-columns "auto 1fr"
:grid-gap [(* 0.5 (:padding theme)) 0]
:border-top-right-radius (:border-radius theme)
:border-bottom-right-radius (:border-radius theme)
:border-right [1 :solid (::c/border theme)]
:border-top [1 :solid (::c/border theme)]
:border-bottom [1 :solid (::c/border theme)]}}
(->> trace
(map-indexed
analyze-trace-item)
(filter
(fn [line]
(let [{:keys [clj? ns]} (meta line)]
(if (:expanded? options) true
(and clj? (not (or
(str/starts-with? (str ns) "nrepl.middleware")
(str/starts-with? (str ns) "clojure.lang.Compiler"))))))))
(partition-by (comp :file meta))
(map-indexed
(fn [index trace]
^{:key index}
[select/with-position
{:column 0 :row index}
[ins/with-key
index
[ins/inspector
{:portal.viewer/inspector {:wrapper wrapper}
:portal.viewer/default :portal.viewer/sub-trace}
(with-meta trace (meta (first trace)))]]])))]]))
(defn- inspect-via [value]
(let [value' (::f/value (meta value) value)
theme (theme/use-theme)
{:keys [type message]} (last (:via value'))
message (or (:cause value') message)]
[d/div
{:style
{:display :flex
:justify-content :space-between
:align-items :center
:box-sizing :border-box}}
[d/div
{:style
{:font-weight :bold
:white-space :pre-bold
:color (::c/exception theme)
:padding [(:padding theme) (* 2 (:padding theme))]}}
[ins/highlight-words
(if message
message
(pr-str (:phase value type)))]]
[d/div
{:style {:display :flex
:align-items :stretch
:justify-content :space-between}}
[d/div
{:style {:display :flex
:align-items :center
:padding [(:padding theme) (* 2 (:padding theme))]}}
[ins/highlight-words
(when message (str (:phase value type)))]]
(when-let [value (:runtime value)]
[d/div
{:style {:padding (:padding theme)
:display :flex
:align-items :center
:box-sizing :border-box
:color (::c/exception theme)
:border-left [1 :solid (::c/exception theme)]}}
[log/icon value (::c/exception theme)]])]]))
(defn inspect-exception [value]
(let [theme (theme/use-theme)
options (ins/use-options)
expanded? (:expanded? options)]
[d/div
{:style
{:color (::c/text theme)
:font-size (:font-size theme)}}
[d/div
[d/div
{:style
{:display :flex
:position :relative
:color (::c/exception theme)
:border [1 :solid (::c/exception theme)]
:border-top-right-radius (:border-radius theme)
:border-top-left-radius (:border-radius theme)
:border-bottom-right-radius (when-not expanded? (:border-radius theme))
:border-bottom-left-radius (when-not expanded? (:border-radius theme))}}
[d/div
{:style
{:position :absolute
:top 0
:bottom 0
:right 0
:left 0
:opacity 0.15
:background (::c/exception theme)}}]
[d/div
{:style
{:display :flex
:align-items :center
:padding [0 (:padding theme)]
:border-right [1 :solid (::c/exception theme)]}}
[icon/exclamation-triangle {:size "lg"}]]
[ins/toggle-expand {:style {:z-index 50 :padding-left (:padding theme)}}]
[d/div
{:style {:flex "1"}}
[inspect-via value]]]
(when expanded?
[ins/with-collection
value
[ins/inspect-map-k-v (dissoc value :cause :phase :runtime)]])]]))
(def viewer
{:predicate exception?
:component inspect-exception
:name :portal.viewer/ex
:doc "Viewer for datafied exceptions."})
(def trace-viewer
{:predicate trace?
:component inspect-stack-trace
:name :portal.viewer/stack-trace})
(def sub-trace-viewer
{:predicate trace?
:component inspect-sub-trace
:name :portal.viewer/sub-trace})