Skip to content

Migrate Eff to Effect #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2018
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
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"package.json"
],
"dependencies": {
"purescript-eff": "^3.0.0",
"purescript-either": "^3.0.0",
"purescript-maybe": "^3.0.0"
"purescript-maybe": "^3.0.0",
"purescript-effect": "^0.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
-- | This module defines an effect, actions and handlers for working
-- | with JavaScript exceptions.

module Control.Monad.Eff.Exception
( EXCEPTION
, Error
module Control.Monad.Effect.Exception
( Error
, error
, message
, name
Expand All @@ -16,14 +15,11 @@ module Control.Monad.Eff.Exception

import Prelude

import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Effect (Effect)

import Data.Either (Either(..))
import Data.Maybe (Maybe(..))

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

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

Expand Down Expand Up @@ -62,29 +58,27 @@ foreign import stackImpl
-- | error "Expected a non-negative number"
-- | ```
foreign import throwException
:: forall a eff
:: forall a
. Error
-> Eff (exception :: EXCEPTION | eff) a
-> Effect a

-- | Catch an exception by providing an exception handler.
-- |
-- | This handler removes the `EXCEPTION` effect.
-- |
-- | For example:
-- |
-- | ```purescript
-- | main = catchException print do
-- | Console.log "Exceptions thrown in this block will be logged to the console"
-- | ```
foreign import catchException
:: forall a eff
. (Error -> Eff eff a)
-> Eff (exception :: EXCEPTION | eff) a
-> Eff eff a
:: forall a
. (Error -> Effect a)
-> Effect a
-> Effect a

-- | A shortcut allowing you to throw an error in one step. Defined as
-- | `throwException <<< error`.
throw :: forall eff a. String -> Eff (exception :: EXCEPTION | eff) a
throw :: forall a. String -> Effect a
throw = throwException <<< error

-- | Runs an Eff and returns eventual Exceptions as a `Left` value. If the
Expand All @@ -93,8 +87,7 @@ throw = throwException <<< error
-- | For example:
-- |
-- | ```purescript
-- | -- Notice that there is no EXCEPTION effect
-- | main :: forall eff. Eff (console :: CONSOLE, fs :: FS | eff) Unit
-- | main :: forall eff. Effect Unit
-- | main = do
-- | result <- try (readTextFile UTF8 "README.md")
-- | case result of
Expand All @@ -104,5 +97,5 @@ throw = throwException <<< error
-- | Console.error ("Couldn't open README.md. Error was: " <> show error)
-- | ```

try :: forall eff a. Eff (exception :: EXCEPTION | eff) a -> Eff eff (Either Error a)
try :: forall a. Effect a -> Effect (Either Error a)
try action = catchException (pure <<< Left) (Right <$> action)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Control.Monad.Eff.Exception.Unsafe where
module Control.Monad.Effect.Exception.Unsafe where

import Control.Monad.Eff.Exception (Error, error, throwException)
import Control.Monad.Eff.Unsafe (unsafePerformEff)
import Control.Monad.Effect.Exception (Error, error, throwException)
import Control.Monad.Effect.Unsafe (unsafePerformEffect)
import Control.Semigroupoid ((<<<))

-- | Throw an exception in pure code. This function should be used very
-- | sparingly, as it can cause unexpected crashes at runtime.
unsafeThrowException :: forall a. Error -> a
unsafeThrowException = unsafePerformEff <<< throwException
unsafeThrowException = unsafePerformEffect <<< throwException

-- | Defined as `unsafeThrowException <<< error`.
unsafeThrow :: forall a. String -> a
Expand Down