-
Notifications
You must be signed in to change notification settings - Fork 28
/
platform_macros.cljc
57 lines (37 loc) · 1.07 KB
/
platform_macros.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
(ns cli-matic.platform-macros
"## Macros shared between CLJ and CLJS.
Unfortunately:
- Macros must be in a separate file (for CLJS)
- Macros have a different import syntax (CLJS)
- Macros defined in .clj will STILL be loaded by CLJS and will fail big time.
So we have to use a separate namespace and hide everything behind reader conditionals.
Usage is:
#?(:clj [cli-matic.platform-macros :refer [try-catch-all]]\n
:cljs [cli-matic.platform-macros :refer-macros [try-catch-all]]\n )
This was a real PITA, man.
")
#?(:clj
(defmacro try-catch-all
"
This creates a try-catch block that either traps
Throwable on the JVM or :default on Node.
Use:
`(try-catch-all (/ 1 0) (fn [x] 0))`
So both expressions must be surrounded by round parentheses.
"
[f onErr]
`(try (~@f)
(catch Throwable t#
((~@onErr) t#))))
)
#?(:cljs
(defmacro try-catch-all
"
See the .clj docs.
"
[f onErr]
`(try (~@f)
(catch :default t#
(do
((~@onErr) t#)))))
)