From bba5e06086bf0de0b3b72e148735712fba3bb02d Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Sat, 12 Dec 2015 10:26:17 +0100 Subject: [PATCH 1/2] Add a `stack` accessor --- bower.json | 3 ++- docs/Control/Monad/Eff/Exception.md | 8 ++++---- src/Control/Monad/Eff/Exception.js | 8 ++++++++ src/Control/Monad/Eff/Exception.purs | 15 +++++++++++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 4562873..172a563 100644 --- a/bower.json +++ b/bower.json @@ -20,6 +20,7 @@ "package.json" ], "dependencies": { - "purescript-eff": "^0.1.2" + "purescript-eff": "^0.1.2", + "purescript-maybe": "^0.3.5" } } diff --git a/docs/Control/Monad/Eff/Exception.md b/docs/Control/Monad/Eff/Exception.md index 45caeca..8941ad7 100644 --- a/docs/Control/Monad/Eff/Exception.md +++ b/docs/Control/Monad/Eff/Exception.md @@ -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` @@ -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 @@ -30,7 +30,7 @@ Show Error error :: String -> Error ``` -Create a Javascript error, specifying a message +Create a JavaScript error, specifying a message #### `message` @@ -38,7 +38,7 @@ Create a Javascript error, specifying a message message :: Error -> String ``` -Get the error message from a Javascript error +Get the error message from a JavaScript error #### `throwException` diff --git a/src/Control/Monad/Eff/Exception.js b/src/Control/Monad/Eff/Exception.js index 8a61454..2b608df 100644 --- a/src/Control/Monad/Eff/Exception.js +++ b/src/Control/Monad/Eff/Exception.js @@ -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; diff --git a/src/Control/Monad/Eff/Exception.purs b/src/Control/Monad/Eff/Exception.purs index 786b512..8fc5138 100644 --- a/src/Control/Monad/Eff/Exception.purs +++ b/src/Control/Monad/Eff/Exception.purs @@ -1,5 +1,5 @@ -- | This module defines an effect, actions and handlers for working --- | with Javascript exceptions. +-- | with JavaScript exceptions. module Control.Monad.Eff.Exception ( EXCEPTION() @@ -12,12 +12,13 @@ module Control.Monad.Eff.Exception ) 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 @@ -25,12 +26,18 @@ instance showError :: Show Error where 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: From da27bce1caed446d7845870f2fddacada2631ecf Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Sat, 12 Dec 2015 10:28:19 +0100 Subject: [PATCH 2/2] Export `stack` --- docs/Control/Monad/Eff/Exception.md | 8 ++++++++ src/Control/Monad/Eff/Exception.purs | 1 + 2 files changed, 9 insertions(+) diff --git a/docs/Control/Monad/Eff/Exception.md b/docs/Control/Monad/Eff/Exception.md index 8941ad7..75c2f95 100644 --- a/docs/Control/Monad/Eff/Exception.md +++ b/docs/Control/Monad/Eff/Exception.md @@ -40,6 +40,14 @@ message :: Error -> String Get the error message from a JavaScript error +#### `stack` + +``` purescript +stack :: Error -> Maybe String +``` + +Get the stack trace from a JavaScript error + #### `throwException` ``` purescript diff --git a/src/Control/Monad/Eff/Exception.purs b/src/Control/Monad/Eff/Exception.purs index 8fc5138..989115c 100644 --- a/src/Control/Monad/Eff/Exception.purs +++ b/src/Control/Monad/Eff/Exception.purs @@ -6,6 +6,7 @@ module Control.Monad.Eff.Exception , Error() , error , message + , stack , throwException , catchException , throw