polysemy-1.9.0.0: Higher-order, low-boilerplate free monads.
Safe HaskellSafe-Inferred
LanguageHaskell2010

Polysemy.Error

Description

 
Synopsis

Effect

data Error e m a where Source #

This effect abstracts the throwing and catching of errors, leaving it up to the interpreter whether to use exceptions or monad transformers like ExceptT to perform the short-circuiting mechanism.

Constructors

Throw :: e -> Error e m a

Short-circuit the current program using the given error value.

Catch :: forall e m a. m a -> (e -> m a) -> Error e m a

Recover from an error that might have been thrown in the higher-order action given by the first argument by passing the error to the handler given by the second argument.

Actions

throw :: forall e r a. Member (Error e) r => e -> Sem r a Source #

Short-circuit the current program using the given error value.

catch :: forall e r a. Member (Error e) r => Sem r a -> (e -> Sem r a) -> Sem r a Source #

Recover from an error that might have been thrown in the higher-order action given by the first argument by passing the error to the handler given by the second argument.

fromEither :: Member (Error e) r => Either e a -> Sem r a Source #

Upgrade an Either into an Error effect.

Since: 0.5.1.0

fromEitherM :: forall e m r a. (Member (Error e) r, Member (Embed m) r) => m (Either e a) -> Sem r a Source #

A combinator doing embed and fromEither at the same time. Useful for interoperating with IO.

Since: 0.5.1.0

fromException :: forall e r a. (Exception e, Member (Error e) r, Member (Embed IO) r) => IO a -> Sem r a Source #

Lift an exception generated from an IO action into an Error.

fromExceptionVia :: (Exception exc, Member (Error err) r, Member (Embed IO) r) => (exc -> err) -> IO a -> Sem r a Source #

Like fromException, but with the ability to transform the exception before turning it into an Error.

fromExceptionSem :: forall e r a. (Exception e, Member (Error e) r, Member (Final IO) r) => Sem r a -> Sem r a Source #

Run a Sem r action, converting any IO exception generated by it into an Error.

fromExceptionSemVia :: (Exception exc, Member (Error err) r, Member (Final IO) r) => (exc -> err) -> Sem r a -> Sem r a Source #

Like fromExceptionSem, but with the ability to transform the exception before turning it into an Error.

note :: Member (Error e) r => e -> Maybe a -> Sem r a Source #

Attempt to extract a Just a from a Maybe a, throwing the provided exception upon Nothing.

try :: Member (Error e) r => Sem r a -> Sem r (Either e a) Source #

Similar to catch, but returns an Either result which is (Right a) if no exception of type e was thrown, or (Left ex) if an exception of type e was thrown and its value is ex.

tryJust :: Member (Error e) r => (e -> Maybe b) -> Sem r a -> Sem r (Either b a) Source #

A variant of try that takes an exception predicate to select which exceptions are caught (c.f. catchJust). If the exception does not match the predicate, it is re-thrown.

catchJust Source #

Arguments

:: Member (Error e) r 
=> (e -> Maybe b)

Predicate to select exceptions

-> Sem r a

Computation to run

-> (b -> Sem r a)

Handler

-> Sem r a 

The function catchJust is like catch, but it takes an extra argument which is an exception predicate, a function which selects which type of exceptions we're interested in.

Interpretations

runError :: Sem (Error e ': r) a -> Sem r (Either e a) Source #

Run an Error effect in the style of ExceptT.

mapError :: forall e1 e2 r a. Member (Error e2) r => (e1 -> e2) -> Sem (Error e1 ': r) a -> Sem r a Source #

Transform one Error into another. This function can be used to aggregate multiple errors into a single type.

Since: 1.0.0.0

errorToIOFinal :: forall e r a. Member (Final IO) r => Sem (Error e ': r) a -> Sem r (Either e a) Source #

Run an Error effect as an IO Exception through final IO. This interpretation is significantly faster than runError.

Beware: Effects that aren't interpreted in terms of IO will have local state semantics in regards to Error effects interpreted this way. See Final.

Since: 1.2.0.0