-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoptoad.clj
69 lines (61 loc) · 2 KB
/
hoptoad.clj
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
(ns org.dipert.hoptoad
"Hoptoad notifications for Clojure apps")
;; (spit "/tmp/lol"
;; (binding [*prxml-indent* 2]
;; (with-out-str
;; (prxml [:decl! "1.0"] (make-notice "1234" (Exception. "LOL!" ))))))
(def notifier
{:name "Clojure Hoptoad Notifier"
:version "1.0.0"
:url "http://github.com/alandipert/hoptoad"})
(defn make-notice [api-key exception]
[:notice {:version "2.0"}
[:api-key api-key]
[:notifier
[:name "Clojure Hoptoad Notifier"]
[:version "1.0.0"]
[:url "http://github.com/alandipert/hoptoad"]]
(exception->error exception)
[:request
[:url "http://bob.net/lol"]
[:component]
[:action]
[:cgi-data
[:var {:key "SERVER_NAME"} "bob.net"]
[:var {:key "HTTP_USER_AGENT"} "Mozilla"]]]
[:server-environment
[:project-root "/testapp"]
[:environment-name "production"]]])
(defn backtrace-scrape? [s]
(and (sequential? s)
(every? map? s)
(every? #(= #{:method :file :number} (set (keys %))) s)))
(defn scrape-backtrace
"Returns a lazy seq of backtrace maps, each with method, line number,
and file name"
[e]
{:pre [(instance? Throwable e)]
:post [(backtrace-scrape? %)]}
(map #(hash-map
:method (.getMethodName %)
:number (.getLineNumber %)
:file (str (.getClassName %) "(" (.getFileName %) ")"))
(.getStackTrace e)))
(defn build-lines
"Returns the line elements for the backtrace."
[trace-map-seq]
{:pre [(backtrace-scrape? trace-map-seq)]
:post [(every? vector? %)
(every? (fn [elem]
(and (= (first elem) :line)
(map? (second elem))))
%)]}
(map #(vector :line %) trace-map-seq))
(defn exception->error
"Returns a full error element with backtrace."
[e]
{:pre [(instance? Throwable e)]
:post [(= :error (first %))
(= :backtrace (first (second %)))]}
[:error
(into [:backtrace] (-> e scrape-backtrace build-lines))])