-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy patherror_kit.clj
More file actions
46 lines (39 loc) · 1.57 KB
/
error_kit.clj
File metadata and controls
46 lines (39 loc) · 1.57 KB
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
; example inspired by http://gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html
(ns examples.error-kit
(:use [clojure.contrib.error-kit]))
(deferror malformed-log-entry [] [msg]
{:msg msg
:unhandled (throw-msg IllegalArgumentException)})
; imaginary log message format:
; 2008-10-05 12:14:00 WARN Some warning message here...
(defn parse-log-entry [entry]
(or
(next (re-matches #"(\d+-\d+-\d+) (\d+:\d+:\d+) (\w+) (.*)" entry))
(raise malformed-log-entry entry)))
(def bad-log
["2008-10-05 12:14:00 WARN Some warning message here..."
"<<this is not a log message>>"
"2008-10-05 12:14:00 INFO End of the current log..."])
(def good-log
["2008-10-05 12:14:00 WARN Some warning message here..."
"2008-10-05 12:14:00 INFO End of the current log..."])
; Example 1. Continue calculation with replacement value
(defn parse-or-nil [logseq]
(with-handler
(vec (map parse-log-entry logseq))
(handle malformed-log-entry [msg]
(continue-with nil))))
; Example 2. Continue calculation with logging & replacement value
(defn parse-or-warn [logseq]
(with-handler
(vec (map parse-log-entry logseq))
(handle malformed-log-entry [msg]
(continue-with (println "****warning****: invalid log: " msg)))))
; Example 3. Caller can choose from a fixed set of contiue strategies.
(defn parse-or-continue [logseq]
(let [parse-log-entry
(fn [entry]
(with-handler (parse-log-entry entry)
(bind-continue skip [msg] nil)
(bind-continue log [msg] (println "****warning****: invalid log: " msg))))]
(vec (map parse-log-entry logseq))))