|
| 1 | +(ns lang-clojure-eval.error |
| 2 | + (:require [clojure.string :as str] |
| 3 | + [sci.core :as sci])) |
| 4 | + |
| 5 | +(defn ruler [title] |
| 6 | + (println (apply str "----- " title " " (repeat (- 50 7 (count title)) \-)))) |
| 7 | + |
| 8 | +(defn split-stacktrace [stacktrace verbose?] |
| 9 | + (if verbose? [stacktrace] |
| 10 | + (let [stack-count (count stacktrace)] |
| 11 | + (if (<= stack-count 10) |
| 12 | + [stacktrace] |
| 13 | + [(take 5 stacktrace) |
| 14 | + (drop (- stack-count 5) stacktrace)])))) |
| 15 | + |
| 16 | +(defn print-stacktrace |
| 17 | + [stacktrace {:keys [:verbose?]}] |
| 18 | + (let [stacktrace (sci/format-stacktrace stacktrace) |
| 19 | + segments (split-stacktrace stacktrace verbose?) |
| 20 | + [fst snd] segments] |
| 21 | + (run! #(print % "\n") fst) |
| 22 | + (when snd |
| 23 | + (print "...\n") |
| 24 | + (run! #(print % "\n") snd)))) |
| 25 | + |
| 26 | +(defn error-context [source ex] |
| 27 | + (let [{:keys [:line :column]} (ex-data ex)] |
| 28 | + (when line |
| 29 | + (when-let [content source] |
| 30 | + (let [matching-line (dec line) |
| 31 | + start-line (max (- matching-line 4) 0) |
| 32 | + end-line (+ matching-line 6) |
| 33 | + [before after] (->> |
| 34 | + (str/split-lines content) |
| 35 | + (map-indexed list) |
| 36 | + (drop start-line) |
| 37 | + (take (- end-line start-line)) |
| 38 | + (split-at (inc (- matching-line start-line)))) |
| 39 | + snippet-lines (concat before |
| 40 | + [[nil (str (str/join "" (repeat (dec column) " ")) |
| 41 | + (str "^--- " (ex-message ex)))]] |
| 42 | + after) |
| 43 | + indices (map first snippet-lines) |
| 44 | + max-size (reduce max 0 (map (comp count str) indices)) |
| 45 | + snippet-lines (map (fn [[idx line]] |
| 46 | + (if idx |
| 47 | + (let [line-number (inc idx)] |
| 48 | + (str (.padStart (str line-number ":") max-size "0") " " line)) |
| 49 | + (str (str/join (repeat (+ 2 max-size) " ")) line))) |
| 50 | + snippet-lines)] |
| 51 | + (str/join "\n" snippet-lines)))))) |
| 52 | + |
| 53 | +(defn right-pad [s n] |
| 54 | + (let [n (- n (count s))] |
| 55 | + (str s (str/join (repeat n " "))))) |
| 56 | + |
| 57 | +(defn print-locals [locals] |
| 58 | + (let [max-name-length (reduce max 0 (map (comp count str) |
| 59 | + (keys locals))) |
| 60 | + max-name-length (+ max-name-length 2)] |
| 61 | + (println |
| 62 | + (with-out-str (binding [*print-length* 10 |
| 63 | + *print-level* 2] |
| 64 | + (doseq [[k v] locals] |
| 65 | + (print (str (right-pad (str k ": ") max-name-length))) |
| 66 | + ;; print nil as nil |
| 67 | + (prn v))))))) |
| 68 | + |
| 69 | +(defn error-handler [source ex] |
| 70 | + (let [stacktrace (sci/stacktrace ex) |
| 71 | + d (ex-data ex) |
| 72 | + sci-error? (isa? (:type d) :sci/error)] |
| 73 | + (ruler "Error") |
| 74 | + (when-let [name (.-name ex)] |
| 75 | + (when-not (= "Error" name) |
| 76 | + (println "Type: " name))) |
| 77 | + (when-let [m (.-message ex)] |
| 78 | + (println (str "Message: " m))) |
| 79 | + (let [{:keys [:file :line :column]} d] |
| 80 | + (when line |
| 81 | + (println (str "Location: " |
| 82 | + (when file (str file ":")) |
| 83 | + line ":" column "")))) |
| 84 | + (when-let [phase (:phase d)] |
| 85 | + (println "Phase: " phase)) |
| 86 | + (when-let [ec (when sci-error? |
| 87 | + (error-context source ex))] |
| 88 | + (println) |
| 89 | + (ruler "Context") |
| 90 | + (println ec)) |
| 91 | + (when sci-error? |
| 92 | + (when-let |
| 93 | + [st (let [st (with-out-str |
| 94 | + (when stacktrace |
| 95 | + (print-stacktrace stacktrace nil)))] |
| 96 | + (when-not (str/blank? st) st))] |
| 97 | + (println) |
| 98 | + (ruler "Stack trace") |
| 99 | + (println st))))) |
0 commit comments