-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.cljc
152 lines (123 loc) · 3.82 KB
/
error.cljc
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(ns promisespromises.error
(:refer-clojure :exclude [ex-info])
(:require
[clojure.core :as clj]
#?(:clj [clojure.pprint :as pprint])
[promisespromises.error.protocols :as pt]))
(defn ex-info
([type]
(ex-info type {} nil))
([type map]
(ex-info type map nil))
([type map cause]
(clj/ex-info
(str type)
(assoc map :error/type type)
cause)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; define UncaughtErrorWrapper and CaughtErrorWrapper types
;;; to wrap javascript errors, preventing auto-rejection of promises
;;; when returnin an error as the value
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UncaughtErrorWrapper is an error which is wrapped with the intention
;; that it should be eventually rethrown (when unwrapped)
(deftype UncaughtErrorWrapper [err]
pt/IErrorWrapper
(-unwrap [_] (throw err))
(-unwrap-value [_] err)
;; #?@(:clj [clojure.lang.IDeref
;; (deref [_] err)]
;; :cljs [IDeref
;; (-deref [_] err)])
)
#?(:clj
(defn print-uncaught-error-wrapper
[uew ^java.io.Writer w]
(.write w "#UncaughtErrorWrapper [")
(print-method (pt/-unwrap uew))
(.write w "]")))
#?(:clj
(defmethod print-method UncaughtErrorWrapper [this ^java.io.Writer w]
(print-uncaught-error-wrapper this w)))
#?(:clj
(defmethod print-dup UncaughtErrorWrapper [this ^java.io.Writer w]
(print-uncaught-error-wrapper this w)))
#?(:clj
(.addMethod pprint/simple-dispatch
UncaughtErrorWrapper
(fn [uew]
(print-uncaught-error-wrapper uew *out*))))
#?(:cljs
(extend-protocol IPrintWithWriter
UncaughtErrorWrapper
(-pr-writer [uew writer _]
(write-all writer "#UncaughtErrorWrapper[" (pt/-unwrap uew) "]"))))
;; CaughtErrorWrapper is an error which is wrapped with the intention
;; that it should be treated as a value and *not* rethrown when unwrapped
(deftype CaughtErrorWrapper [err]
pt/IErrorWrapper
(-unwrap [_] err)
(-unwrap-value [_] err)
;; #?@(:clj [clojure.lang.IDeref
;; (deref [_] err)]
;; :cljs [IDeref
;; (-deref [_] err)])
)
#?(:clj
(defn print-caught-error-wrapper
[uew ^java.io.Writer w]
(.write w "#CaughtErrorWrapper [")
(print-method (pt/-unwrap uew))
(.write w "]")))
#?(:clj
(defmethod print-method CaughtErrorWrapper [this ^java.io.Writer w]
(print-caught-error-wrapper this w)))
#?(:clj
(defmethod print-dup CaughtErrorWrapper [this ^java.io.Writer w]
(print-caught-error-wrapper this w)))
#?(:clj
(.addMethod pprint/simple-dispatch
CaughtErrorWrapper
(fn [uew]
(print-caught-error-wrapper uew *out*))))
#?(:cljs
(extend-protocol IPrintWithWriter
CaughtErrorWrapper
(-pr-writer [uew writer _]
(write-all writer "#CaughtErrorWrapper[" (pt/-unwrap uew) "]"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; implement UncaughtErrorWrapper and CaughtErrorWrapper types
;;; behaviours
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(extend-protocol pt/IErrorWrapper
#?(:clj Object :cljs default)
(-unwrap [this] this)
(-unwrap-value [this] this)
nil
(-unwrap [_this] nil)
(-unwrap-value [_this] nil))
(defn uncaught-wrapper?
[v]
(instance? UncaughtErrorWrapper v))
(defn caught-wrapper?
[v]
(instance? CaughtErrorWrapper v))
(defn wrapper?
[v]
(or
(instance? UncaughtErrorWrapper v)
(instance? CaughtErrorWrapper v)))
(defn wrap-uncaught
[err]
(UncaughtErrorWrapper. err))
(defn wrap-caught
[err]
(CaughtErrorWrapper. err))
(defn unwrap
"unwrap and maybe throw"
[e]
(pt/-unwrap e))
(defn unwrap-value
"unwrap value, never throwing"
[e]
(pt/-unwrap-value e))