Module

Control.Monad.Eff.Exception

Package
purescript-exceptions
Repository
purescript/purescript-exceptions

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

#EXCEPTION Source

data EXCEPTION :: Effect

This effect is used to annotate code which possibly throws exceptions

#Error Source

data Error :: Type

The type of JavaScript errors

Instances

#error Source

error :: String -> Error

Create a JavaScript error, specifying a message

#message Source

message :: Error -> String

Get the error message from a JavaScript error

#stack Source

stack :: Error -> Maybe String

Get the stack trace from a JavaScript error

#throwException Source

throwException :: forall a eff. Error -> Eff (exception :: EXCEPTION | eff) a

Throw an exception

For example:

main = do
  x <- readNumber
  when (x < 0) $ throwException $
    error "Expected a non-negative number"

#catchException Source

catchException :: forall a eff. (Error -> Eff eff a) -> Eff (exception :: EXCEPTION | eff) a -> Eff eff a

Catch an exception by providing an exception handler.

This handler removes the EXCEPTION effect.

For example:

main = catchException print do
  Console.log "Exceptions thrown in this block will be logged to the console"

#throw Source

throw :: forall eff a. String -> Eff (exception :: EXCEPTION | eff) a

A shortcut allowing you to throw an error in one step. Defined as throwException <<< error.

#try Source

try :: forall eff a. Eff (exception :: EXCEPTION | eff) a -> Eff eff (Either Error a)

Runs an Eff and returns eventual Exceptions as a Left value. If the computation succeeds the result gets wrapped in a Right.

For example:

-- Notice that there is no EXCEPTION effect
main :: forall eff. Eff (console :: CONSOLE, fs :: FS | eff) Unit
main = do
  result <- try (readTextFile UTF8 "README.md")
  case result of
    Right lines ->
      Console.log ("README: \n" <> lines )
    Left error ->
      Console.error ("Couldn't open README.md. Error was: " <> show error)