Skip to content
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
13 changes: 11 additions & 2 deletions Control/Monad/Error/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Control.Monad.Error.Class (
tryError,
withError,
handleError,
onError,
mapError,
modifyError,
) where
Expand All @@ -72,9 +73,9 @@ import qualified Control.Monad.Trans.RWS.CPS as CPSRWS
import qualified Control.Monad.Trans.Writer.CPS as CPSWriter
import Control.Monad.Trans.Class (lift)
import Control.Exception (IOException, catch, ioError)
import Control.Monad (Monad)
import Control.Monad (Monad ((>>=), (>>)))
import Data.Monoid (Monoid)
import Prelude (Either (Left, Right), Maybe (Nothing), either, flip, (.), IO, pure, (<$>), (>>=))
import Prelude (Either (Left, Right), Maybe (Nothing), either, flip, (.), IO, pure, (<$>))

{- |
The strategy of combining computations that can throw exceptions
Expand Down Expand Up @@ -227,6 +228,14 @@ withError f action = tryError action >>= either (throwError . f) pure
handleError :: MonadError e m => (e -> m a) -> m a -> m a
handleError = flip catchError

-- | If an action throws an error, run a second action and rethrow the error.
-- If the second action also throws an error, it takes precedence. Do not run
-- the second action at all if the first succeeds.
--
-- @since 2.3.2
onError :: MonadError e m => m a -> m b -> m a
onError action1 action2 = action1 `catchError` \e -> action2 >> throwError e

-- | 'MonadError' analogue of the 'mapExceptT' function. The
-- computation is unwrapped, a function is applied to the @Either@, and
-- the result is lifted into the second 'MonadError' instance.
Expand Down
1 change: 1 addition & 0 deletions Control/Monad/Except.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Control.Monad.Except
Error.tryError,
Error.withError,
Error.handleError,
Error.onError,
Error.mapError,
Error.modifyError,
-- * The ExceptT monad transformer
Expand Down
Loading