lens-5.3.3: Lenses, Folds and Traversals

Copyright | (C) 2013-2016 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Control.Lens.Internal.Exception

Description

This module uses dirty tricks to generate a `Handler` from an arbitrary `Fold`.

Synopsis

- class Handleable e (m :: Type -> Type) (h :: Type -> Type) | h -> e m where
  - handler :: Typeable a => Getting (First a) e a -> (a -> m r) -> h r
  - handler_ :: Typeable a => Getting (First a) e a -> m r -> h r
- data HandlingException = HandlingException

# Documentation

class 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.

Minimal complete definition

handler

Methods

handler :: Typeable a => Getting (First a) e a -> (a -> m r) -> h r #

This builds a `Handler` for just the targets of a given `Prism` (or any `Getter`, really).

    catches ... [ handler _AssertionFailed (s -> print $ "Assertion Failed\n" ++ s)
                , handler _ErrorCall (s -> print $ "Error\n" ++ s)
                ]

This works ith both the `Handler` type provided by `Control.Exception`:

    handler :: Getter     SomeException a -> (a -> IO r) -> Handler r
    handler :: Fold       SomeException a -> (a -> IO r) -> Handler r
    handler :: Prism'     SomeException a -> (a -> IO r) -> Handler r
    handler :: Lens'      SomeException a -> (a -> IO r) -> Handler r
    handler :: Traversal' SomeException a -> (a -> IO r) -> Handler r

and with the `Handler` type provided by `Control.Monad.Catch`:

    handler :: Getter     SomeException a -> (a -> m r) -> Handler m r
    handler :: Fold       SomeException a -> (a -> m r) -> Handler m r
    handler :: Prism'     SomeException a -> (a -> m r) -> Handler m r
    handler :: Lens'      SomeException a -> (a -> m r) -> Handler m r
    handler :: Traversal' SomeException a -> (a -> m r) -> Handler m r

and with the `Handler` type provided by `Control.Monad.Error.Lens`:

    handler :: Getter     e a -> (a -> m r) -> Handler e m r
    handler :: Fold       e a -> (a -> m r) -> Handler e m r
    handler :: Prism'     e a -> (a -> m r) -> Handler e m r
    handler :: Lens'      e a -> (a -> m r) -> Handler e m r
    handler :: Traversal' e a -> (a -> m r) -> Handler e m r

handler_ :: Typeable a => Getting (First a) e a -> m r -> h r #

This builds a `Handler` for just the targets of a given `Prism` (or any `Getter`, really). that ignores its input and just recovers with the stated monadic action.

    catches ... [ handler_ _NonTermination (return "looped")
                , handler_ _StackOverflow (return "overflow")
                ]

This works with the `Handler` type provided by `Control.Exception`:

    handler_ :: Getter     SomeException a -> IO r -> Handler r
    handler_ :: Fold       SomeException a -> IO r -> Handler r
    handler_ :: Prism'     SomeException a -> IO r -> Handler r
    handler_ :: Lens'      SomeException a -> IO r -> Handler r
    handler_ :: Traversal' SomeException a -> IO r -> Handler r

and with the `Handler` type provided by `Control.Monad.Catch`:

    handler_ :: Getter     SomeException a -> m r -> Handler m r
    handler_ :: Fold       SomeException a -> m r -> Handler m r
    handler_ :: Prism'     SomeException a -> m r -> Handler m r
    handler_ :: Lens'      SomeException a -> m r -> Handler m r
    handler_ :: Traversal' SomeException a -> m r -> Handler m r

and with the `Handler` type provided by `Control.Monad.Error.Lens`:

    handler_ :: Getter     e a -> m r -> Handler e m r
    handler_ :: Fold       e a -> m r -> Handler e m r
    handler_ :: Prism'     e a -> m r -> Handler e m r
    handler_ :: Lens'      e a -> m r -> Handler e m r
    handler_ :: Traversal' e a -> m r -> Handler e m r

#### Instances

Instances details

Handleable SomeException IO Handler # |
Instance details Defined in Control.Lens.Internal.Exception Methods handler :: Typeable a => Getting (First a) SomeException a -> (a -> IO r) -> Handler r # handler_ :: Typeable a => Getting (First a) SomeException a -> IO r -> Handler r #
Typeable m => Handleable SomeException m (Handler m) # |
Instance details Defined in Control.Lens.Internal.Exception Methods handler :: Typeable a => Getting (First a) SomeException a -> (a -> m r) -> Handler m r # handler_ :: Typeable a => Getting (First a) SomeException a -> m r -> Handler m r #
Handleable e m (Handler e m) # |
Instance details Defined in Control.Monad.Error.Lens Methods handler :: Typeable a => Getting (First a) e a -> (a -> m r) -> Handler e m r # handler_ :: Typeable a => Getting (First a) e a -> m r -> Handler e m r #

data HandlingException #

There was an `Exception` caused by abusing the internals of a `Handler`.

Constructors

HandlingException |

#### Instances

Instances details

Exception HandlingException # |
Instance details Defined in Control.Lens.Internal.Exception Methods toException :: HandlingException -> SomeException # fromException :: SomeException -> Maybe HandlingException # displayException :: HandlingException -> String # backtraceDesired :: HandlingException -> Bool #
Show HandlingException # |
Instance details Defined in Control.Lens.Internal.Exception Methods showsPrec :: Int -> HandlingException -> ShowS # show :: HandlingException -> String # showList :: [HandlingException] -> ShowS #
AsHandlingException HandlingException # |
Instance details Defined in Control.Exception.Lens Methods __HandlingException :: Prism' HandlingException HandlingException # _HandlingException :: Prism' HandlingException () #