Skip to content

Commit 5bb5831

Browse files
authored
Merge pull request #68 from dawran6/catch-parse-error
Catch parsing markdown error
2 parents bd02030 + 0501cbf commit 5bb5831

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/clojurians_log/message_parser.clj

+33-11
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,19 @@
240240
(>= cursor (:end stack-top))
241241
false)))
242242

243-
(defn parse2 [message]
243+
(defn parse2-inner [message]
244244
(let [matches (->> (match-all-patterns message-patterns message)
245245
(map fix-blockquote-match)
246246
(sort match-compare))]
247247

248248
;; (clojure.pprint/pprint matches)
249249

250250
;; Loop starting from the beginning of the message string...
251-
(loop [iteration 0
251+
(loop [iteration 0
252252
last-cursor 0
253-
matches matches
254-
stack []
255-
result []]
253+
matches matches
254+
stack []
255+
result []]
256256

257257
;; (newline)
258258
;; (println (str "vvvvvvvvvvvvvv Loop start " iteration " vvvvvvvvvvvvvv"))
@@ -265,17 +265,17 @@
265265
;; (println ">> result")
266266
;; (clojure.pprint/pprint result)
267267

268-
(let [item (first matches)
269-
cursor (:start item) ;; Cursor is always the starting position of the item being processed
268+
(let [item (first matches)
269+
cursor (:start item) ;; Cursor is always the starting position of the item being processed
270270
stack-top (last stack)]
271271
(cond
272272
;; Completed processing for all matches?
273273
(empty? matches)
274-
(let [msg-len (count message)
274+
(let [msg-len (count message)
275275
;; _ (clojure.pprint/pprint stack)
276276
[new-stack token cursor] (collapse-match-stack message stack msg-len)
277-
_ (assert (empty? new-stack))
278-
cursor (or cursor 0)
277+
_ (assert (empty? new-stack))
278+
cursor (or cursor 0)
279279
;; _ (println "result: " (type result))
280280
;; _ (clojure.pprint/pprint result)
281281
]
@@ -312,6 +312,18 @@
312312
(not= last-cursor cursor))
313313
(conj (extract-undecorated-text message last-cursor cursor)))))))))
314314

315+
(defn parse2
316+
"Parse markdown message into hiccup.
317+
318+
Return the un-parsed message if any error happens."
319+
[message]
320+
(try
321+
(parse2-inner message)
322+
(catch Exception ex
323+
(println "Failed to parse message:" message)
324+
#_(.printStackTrace ex)
325+
[[:undecorated message]])))
326+
315327
(defn parse-with-pattern [pattern-k message]
316328
(let [pattern (get message-patterns pattern-k)]
317329
(re-seq-pos pattern message)))
@@ -330,4 +342,14 @@
330342
doall))
331343
nil)
332344

333-
)
345+
(let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")]
346+
(->> data
347+
(map #(parse2 (:message/text %)))
348+
doall))
349+
350+
(with-redefs-fn {#'parse2-inner (fn [_] (throw (ex-info "error" {})))}
351+
(fn [] (let [data (clojurians-log.db.queries/channel-day-messages (user/db) "clojure" "2018-02-02")]
352+
(->> data
353+
(map #(parse2 (:message/text %)))
354+
doall))))
355+
)

0 commit comments

Comments
 (0)