HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelens-5.3.5Haskell2010

Control.Monad.Error.Lens

  • 1 type
  • 1 class
  • 8 values
  • Packagelens-5.3.5
  • Exports10
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceLens.hs

Catching

2 declarations
valuecatching
  1. :: MonadError e m
  2. => Getting (First a) e a
  3. -> m r
  4. -> a -> m r
  5. -> m r
#

Catch exceptions that match a given Prism (or any Getter, really).

catching :: MonadError e m => Prism' e a     -> m r -> (a -> m r) -> m r
catching :: MonadError e m => Lens' e a      -> m r -> (a -> m r) -> m r
catching :: MonadError e m => Traversal' e a -> m r -> (a -> m r) -> m r
catching :: MonadError e m => Iso' e a       -> m r -> (a -> m r) -> m r
catching :: MonadError e m => Getter e a    -> m r -> (a -> m r) -> m r
catching :: MonadError e m => Fold e a      -> m r -> (a -> m r) -> m r
valuecatching_ :: MonadError e m => Getting (First a) e a -> m r -> m r -> m r
#

Catch exceptions that match a given Prism (or any Getter), discarding the information about the match. This is particularly useful when you have a Prism' e () where the result of the Prism or Fold isn't particularly valuable, just the fact that it matches.

catching_ :: MonadError e m => Prism' e a     -> m r -> m r -> m r
catching_ :: MonadError e m => Lens' e a      -> m r -> m r -> m r
catching_ :: MonadError e m => Traversal' e a -> m r -> m r -> m r
catching_ :: MonadError e m => Iso' e a       -> m r -> m r -> m r
catching_ :: MonadError e m => Getter e a    -> m r -> m r -> m r
catching_ :: MonadError e m => Fold e a      -> m r -> m r -> m r

Handling

2 declarations
valuehandling
  1. :: MonadError e m
  2. => Getting (First a) e a
  3. -> a -> m r
  4. -> m r
  5. -> m r
#

A version of catching with the arguments swapped around; useful in situations where the code for the handler is shorter.

handling :: MonadError e m => Prism' e a     -> (a -> m r) -> m r -> m r
handling :: MonadError e m => Lens' e a      -> (a -> m r) -> m r -> m r
handling :: MonadError e m => Traversal' e a -> (a -> m r) -> m r -> m r
handling :: MonadError e m => Iso' e a       -> (a -> m r) -> m r -> m r
handling :: MonadError e m => Fold e a      -> (a -> m r) -> m r -> m r
handling :: MonadError e m => Getter e a    -> (a -> m r) -> m r -> m r
valuehandling_ :: MonadError e m => Getting (First a) e a -> m r -> m r -> m r
#

A version of catching_ with the arguments swapped around; useful in situations where the code for the handler is shorter.

handling_ :: MonadError e m => Prism' e a     -> m r -> m r -> m r
handling_ :: MonadError e m => Lens' e a      -> m r -> m r -> m r
handling_ :: MonadError e m => Traversal' e a -> m r -> m r -> m r
handling_ :: MonadError e m => Iso' e a       -> m r -> m r -> m r
handling_ :: MonadError e m => Getter e a    -> m r -> m r -> m r
handling_ :: MonadError e m => Fold e a      -> m r -> m r -> m r

Trying

1 declaration
valuetrying :: MonadError e m => Getting (First a) e a -> m r -> m (Either a r)
#

trying takes a Prism (or any Getter) to select which exceptions are caught If the exception does not match the predicate, it is re-thrown.

trying :: MonadError e m => Prism' e a     -> m r -> m (Either a r)
trying :: MonadError e m => Lens' e a      -> m r -> m (Either a r)
trying :: MonadError e m => Traversal' e a -> m r -> m (Either a r)
trying :: MonadError e m => Iso' e a       -> m r -> m (Either a r)
trying :: MonadError e m => Getter e a    -> m r -> m (Either a r)
trying :: MonadError e m => Fold e a      -> m r -> m (Either a r)

Handlers

3 declarations
valuecatches :: MonadError e m => m a -> [Handler e m a] -> m a
#

This function exists to remedy a gap between the functionality of Control.Exception and Control.Monad.Error. Control.Exception supplies catches and a notion of Handler, which we duplicate here in a form suitable for working with any MonadError instance.

Sometimes you want to catch two different sorts of error. You could do something like

f = handling _Foo handleFoo (handling _Bar handleBar expr)

However, there are a couple of problems with this approach. The first is that having two exception handlers is inefficient. However, the more serious issue is that the second exception handler will catch exceptions in the first, e.g. in the example above, if handleFoo uses throwError then the second exception handler will catch it.

Instead, we provide a function catches, which would be used thus:

f = catches expr [ handler _Foo handleFoo
                 , handler _Bar handleBar
                 ]
datadata Handler e (m :: Type -> Type) r
#

You need this when using catches.

Constructors

Instances6Handleable, Functor, Alt, Plus, Semigroup, Monoid
classclass Handleable e (m :: Type -> Type) (h :: Type -> Type) | h -> e m where
#

Both exceptions and Control.Exception provide a Handler type.

This lets us write combinators to build handlers that are agnostic about the choice of which of these they use.

Methods

Instances3Handleable

Throwing

2 declarations