Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"package.json"
],
"dependencies": {
"purescript-eff": "^0.1.2"
"purescript-eff": "^0.1.2",
"purescript-maybe": "^0.3.5"
}
}
16 changes: 12 additions & 4 deletions docs/Control/Monad/Eff/Exception.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Module Control.Monad.Eff.Exception

This module defines an effect, actions and handlers for working
with Javascript exceptions.
with JavaScript exceptions.

#### `EXCEPTION`

Expand All @@ -17,7 +17,7 @@ This effect is used to annotate code which possibly throws exceptions
data Error :: *
```

The type of Javascript errors
The type of JavaScript errors

##### Instances
``` purescript
Expand All @@ -30,15 +30,23 @@ Show Error
error :: String -> Error
```

Create a Javascript error, specifying a message
Create a JavaScript error, specifying a message

#### `message`

``` purescript
message :: Error -> String
```

Get the error message from a Javascript error
Get the error message from a JavaScript error

#### `stack`

``` purescript
stack :: Error -> Maybe String
```

Get the stack trace from a JavaScript error

#### `throwException`

Expand Down
8 changes: 8 additions & 0 deletions src/Control/Monad/Eff/Exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ exports.message = function (e) {
return e.message;
};

exports.stackImpl = function (just) {
return function (nothing) {
return function (e) {
return e.stack ? just(e.stack) : nothing;
};
};
};

exports.throwException = function (e) {
return function () {
throw e;
Expand Down
16 changes: 12 additions & 4 deletions src/Control/Monad/Eff/Exception.purs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
-- | This module defines an effect, actions and handlers for working
-- | with Javascript exceptions.
-- | with JavaScript exceptions.

module Control.Monad.Eff.Exception
( EXCEPTION()
, Error()
, error
, message
, stack
, throwException
, catchException
, throw
) where

import Prelude
import Data.Maybe (Maybe(..))
import Control.Monad.Eff (Eff())

-- | This effect is used to annotate code which possibly throws exceptions
foreign import data EXCEPTION :: !

-- | The type of Javascript errors
-- | The type of JavaScript errors
foreign import data Error :: *

instance showError :: Show Error where
show = showErrorImpl

foreign import showErrorImpl :: Error -> String

-- | Create a Javascript error, specifying a message
-- | Create a JavaScript error, specifying a message
foreign import error :: String -> Error

-- | Get the error message from a Javascript error
-- | Get the error message from a JavaScript error
foreign import message :: Error -> String

-- | Get the stack trace from a JavaScript error
stack :: Error -> Maybe String
stack = stackImpl Just Nothing

foreign import stackImpl :: (forall a. a -> Maybe a) -> (forall a. Maybe a) -> Error -> Maybe String

-- | Throw an exception
-- |
-- | For example:
Expand Down