Skip to content

Commit f785aaf

Browse files
committed
Merge pull request #6 from purescript/docs
Docs, exports
2 parents c5f428e + 81500f7 commit f785aaf

File tree

2 files changed

+99
-12
lines changed

2 files changed

+99
-12
lines changed

README.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,79 @@
22

33
## Module Control.Monad.Eff.Exception
44

5-
### Types
65

7-
data Error :: *
6+
This module defines an effect, actions and handlers for working
7+
with Javascript exceptions.
88

9-
data Exception :: !
9+
#### `Exception`
1010

11+
``` purescript
12+
data Exception :: !
13+
```
1114

12-
### Type Class Instances
15+
This effect is used to annotate code which possibly throws exceptions
1316

14-
instance showError :: Show Error
17+
#### `Error`
1518

19+
``` purescript
20+
data Error :: *
21+
```
1622

17-
### Values
23+
The type of Javascript errors
1824

19-
catchException :: forall a eff. (Error -> Eff eff a) -> Eff (err :: Exception | eff) a -> Eff eff a
25+
#### `showError`
2026

21-
error :: String -> Error
27+
``` purescript
28+
instance showError :: Show Error
29+
```
2230

23-
message :: Error -> String
2431

25-
showErrorImpl :: Error -> String
32+
#### `error`
2633

27-
throwException :: forall a eff. Error -> Eff (err :: Exception | eff) a
34+
``` purescript
35+
error :: String -> Error
36+
```
37+
38+
Create a Javascript error, specifying a message
39+
40+
#### `message`
41+
42+
``` purescript
43+
message :: Error -> String
44+
```
45+
46+
Get the error message from a Javascript error
47+
48+
#### `throwException`
49+
50+
``` purescript
51+
throwException :: forall a eff. Error -> Eff (err :: Exception | eff) a
52+
```
53+
54+
Throw an exception
55+
56+
For example:
57+
58+
```purescript
59+
main = do
60+
x <- readNumber
61+
when (x < 0) $ throwException $
62+
error "Expected a non-negative number"
63+
```
64+
65+
#### `catchException`
66+
67+
``` purescript
68+
catchException :: forall a eff. (Error -> Eff eff a) -> Eff (err :: Exception | eff) a -> Eff eff a
69+
```
70+
71+
Catch an exception by providing an exception handler.
72+
73+
This handler removes the `Exception` effect.
74+
75+
For example:
76+
77+
```purescript
78+
main = catchException print do
79+
trace "Exceptions thrown in this block will be logged to the console"
80+
```

src/Control/Monad/Eff/Exception.purs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
module Control.Monad.Eff.Exception where
1+
-- | This module defines an effect, actions and handlers for working
2+
-- | with Javascript exceptions.
3+
4+
module Control.Monad.Eff.Exception
5+
( Exception()
6+
, Error()
7+
, error
8+
, message
9+
, throwException
10+
, catchException
11+
) where
212

313
import Control.Monad.Eff
414

15+
-- | This effect is used to annotate code which possibly throws exceptions
516
foreign import data Exception :: !
617

18+
-- | The type of Javascript errors
719
foreign import data Error :: *
820

921
instance showError :: Show Error where
@@ -16,20 +28,32 @@ foreign import showErrorImpl
1628
}
1729
""" :: Error -> String
1830

31+
-- | Create a Javascript error, specifying a message
1932
foreign import error
2033
"""
2134
function error(msg) {
2235
return new Error(msg);
2336
}
2437
""" :: String -> Error
2538

39+
-- | Get the error message from a Javascript error
2640
foreign import message
2741
"""
2842
function message(e) {
2943
return e.message;
3044
}
3145
""" :: Error -> String
3246

47+
-- | Throw an exception
48+
-- |
49+
-- | For example:
50+
-- |
51+
-- | ```purescript
52+
-- | main = do
53+
-- | x <- readNumber
54+
-- | when (x < 0) $ throwException $
55+
-- | error "Expected a non-negative number"
56+
-- | ```
3357
foreign import throwException
3458
"""
3559
function throwException(e) {
@@ -39,6 +63,16 @@ foreign import throwException
3963
}
4064
""" :: forall a eff. Error -> Eff (err :: Exception | eff) a
4165

66+
-- | Catch an exception by providing an exception handler.
67+
-- |
68+
-- | This handler removes the `Exception` effect.
69+
-- |
70+
-- | For example:
71+
-- |
72+
-- | ```purescript
73+
-- | main = catchException print do
74+
-- | trace "Exceptions thrown in this block will be logged to the console"
75+
-- | ```
4276
foreign import catchException
4377
"""
4478
function catchException(c) {

0 commit comments

Comments
 (0)