forked from status-im/status-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.cljs
More file actions
57 lines (50 loc) · 2.18 KB
/
error_handler.cljs
File metadata and controls
57 lines (50 loc) · 2.18 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
47
48
49
50
51
52
53
54
55
56
57
(ns status-im.utils.error-handler
(:require [clojure.string :as string]
[status-im.utils.utils :as utils]))
;; Error handling code based on https://gist.github.com/pesterhazy/e6846be1b6712a9038537022d131ce46
(defonce !handler-set (atom false))
(defn downgrade-reagent-errors!
"Downgrade reagent error to warning
Reagent uses console.error to notify the user that an exception occurs while
rendering. Unfortunately, react-native can only show one RedBox at a time and
discards all subsequent ones. This obscures the actual exception. By
downgrading the first screen to a warning, the second screen is actually shown
to the user."
[]
(when-not @!handler-set
(reset! !handler-set true)
(let [original-error (.-error js/console)]
(set! (.-error js/console)
(fn [& [head :as args]]
(if (and (string? head) (string/starts-with? head "Error rendering component"))
(apply (.-warn js/console) "Additional exception info:" args)
(apply original-error args)))))))
(defn format-error [e]
(if (instance? js/Error e)
{:name (.-name e) :message (.-message e) :stack (.-stack e)}
{:message (pr-str e)}))
(defn handle-error [e is-fatal]
(let [f (format-error e)]
(js/console.log (str "PRETTY PRINTED EXCEPTION"
"\n\n***\nNAME: "
(pr-str (:name f))
"\nMESSAGE: "
(:message f)
"\n\n"
(:stack f)
"\n\n***"))))
(defonce !error-handler-set? (atom false))
(defn register-exception-handler!
"Improve error messages printed to console.
When js/goog.DEBUG is false, show a popup with an error summary; else rely on default `red` screen."
[]
(downgrade-reagent-errors!)
(when-not @!error-handler-set?
(reset! !error-handler-set? true)
(let [orig-handler (some-> js/ErrorUtils .-getGlobalHandler (.call))]
(js/ErrorUtils.setGlobalHandler
(fn [e isFatal]
(handle-error e isFatal)
(if js/goog.DEBUG
(some-> orig-handler (.call nil e isFatal))
(utils/show-popup "Error" (.-message e))))))))