forked from TonyGen/mongoDB-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slight API refactoring. Fix spinning pipeline when other end disconne…
…cts. Handle response flags correctly
- Loading branch information
Tony Hannan
committed
Jul 27, 2010
1 parent
6435bc3
commit 3a7f235
Showing
13 changed files
with
286 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{- | This is just like "Control.Monad.Error.Class" except you can throw/catch the error of any ErrorT in the monad stack instead of just the top one as long as the error types are different. If two or more ErrorTs in the stack have the same error type you get the error of the top one. -} | ||
|
||
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, OverlappingInstances #-} | ||
|
||
module Control.Monad.Throw where | ||
|
||
import Prelude hiding (catch) | ||
import Control.Monad.Reader | ||
import Control.Monad.Error | ||
|
||
-- | Same as 'MonadError' but without functional dependency so the same monad can have multiple errors with different types | ||
class (Monad m) => Throw e m where | ||
throw :: e -> m a | ||
-- ^ Abort action and throw give exception. Analogous to 'throwError'. | ||
catch :: m a -> (e -> m a) -> m a | ||
-- ^ If first action aborts with exception then execute second action. Analogous to 'catchError' | ||
|
||
throwLeft :: (Throw e m) => m (Either e a) -> m a | ||
-- ^ Execute action and throw exception if result is Left, otherwise return the Right result | ||
throwLeft = (either throw return =<<) | ||
|
||
instance (Error e) => Throw e (Either e) where | ||
throw = throwError | ||
catch = catchError | ||
|
||
instance (Error e, Monad m) => Throw e (ErrorT e m) where | ||
throw = throwError | ||
catch = catchError | ||
|
||
instance (Error e, Throw e m, Error x) => Throw e (ErrorT x m) where | ||
throw = lift . throw | ||
catch a h = ErrorT $ catch (runErrorT a) (runErrorT . h) | ||
|
||
instance (Throw e m) => Throw e (ReaderT x m) where | ||
throw = lift . throw | ||
catch a h = ReaderT $ \x -> catch (runReaderT a x) (flip runReaderT x . h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.