Skip to content

Commit b45edc4

Browse files
committed
Merge pull request #1 from purescript/error
Use Error type for exceptions
2 parents d6cfde8 + 5c442e1 commit b45edc4

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44

55
### Types
66

7-
data Exception :: * -> !
7+
data Error :: *
8+
9+
data Exception :: !
10+
11+
12+
### Type Class Instances
13+
14+
instance showError :: Show Error
815

916

1017
### Values
1118

12-
catchException :: forall e r a. (e -> Eff r a) -> Eff (err :: Exception e | r) a -> Eff r a
19+
catchException :: forall a eff. (Error -> Eff eff a) -> Eff (err :: Exception | eff) a -> Eff eff a
20+
21+
error :: String -> Error
22+
23+
message :: Error -> String
24+
25+
showErrorImpl :: Error -> String
1326

14-
throwException :: forall a e r. e -> Eff (err :: Exception e | r) a
27+
throwException :: forall a eff. Error -> Eff (err :: Exception | eff) a

src/Control/Monad/Eff/Exception.purs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@ module Control.Monad.Eff.Exception where
22

33
import Control.Monad.Eff
44

5-
foreign import data Exception :: * -> !
5+
foreign import data Exception :: !
66

7+
foreign import data Error :: *
8+
9+
instance showError :: Show Error where
10+
show = showErrorImpl
11+
12+
foreign import showErrorImpl
13+
"function showErrorImpl(err) {\
14+
\ return err.stack ? err.stack : err.toString();\
15+
\}" :: Error -> String
16+
17+
foreign import error
18+
"function error(msg) {\
19+
\ return new Error(msg);\
20+
\};" :: String -> Error
21+
22+
foreign import message
23+
"function message(e) {\
24+
\ return e.message;\
25+
\}" :: Error -> String
26+
727
foreign import throwException
828
"function throwException(e) {\
929
\ return function() {\
1030
\ throw e;\
1131
\ };\
12-
\}" :: forall a e r. e -> Eff (err :: Exception e | r) a
32+
\}" :: forall a eff. Error -> Eff (err :: Exception | eff) a
1333

1434
foreign import catchException
1535
"function catchException(c) {\
@@ -18,8 +38,12 @@ foreign import catchException
1838
\ try {\
1939
\ return t();\
2040
\ } catch(e) {\
21-
\ return c(e)();\
41+
\ if (e instanceof Error) {\
42+
\ return c(e)();\
43+
\ } else {\
44+
\ throw e;\
45+
\ }\
2246
\ }\
2347
\ };\
2448
\ };\
25-
\}" :: forall e r a. (e -> Eff r a) -> Eff (err :: Exception e | r) a -> Eff r a
49+
\}" :: forall a eff. (Error -> Eff eff a) -> Eff (err :: Exception | eff) a -> Eff eff a

0 commit comments

Comments
 (0)