safe-failure 0.1 → 0.2
raw patch · 2 files changed
+28/−14 lines, 2 filesdep +transformersdep −extensible-exceptionsdep ~base
Dependencies added: transformers
Dependencies removed: extensible-exceptions
Dependency ranges changed: base
Files
- Safe/Failure.hs +25/−0
- safe-failure.cabal +3/−14
Safe/Failure.hs view
@@ -31,6 +31,10 @@ Safe.Failure.read, -- * Useful combinators def, note,+-- * Assertions+Safe.Failure.assert,+-- * IO functions+Safe.Failure.readFile, -- * Exceptions SafeException(..), HeadFailure(..), TailFailure(..), InitFailure(..), LastFailure(..),@@ -45,9 +49,11 @@ import Control.Monad.Failure import Data.Maybe import Data.Typeable+import Control.Monad.Trans #ifdef CME import Control.Monad.Exception.Throws #endif+import Control.Monad (liftM) {-| @def@, use it to return a default value in the event of an error. @@ -198,6 +204,25 @@ lookup :: (Eq a, MonadFailure (LookupFailure a) m) => a -> [(a,b)] -> m b lookup key = maybe (failure (LookupFailure key)) return . Prelude.lookup key +-- | Assert a value to be true. If true, returns the first value as a succss.+-- Otherwise, returns the second value as a failure.+assert :: (MonadFailure e m, Exception e)+ => Bool+ -> v+ -> e+ -> m v+assert b v e = if b then return v else failure e++-- | The standard readFile function with any 'IOException's returned as a+-- failure instead of a runtime exception.+readFile :: (MonadFailure IOException m, MonadIO m) => FilePath -> m String+readFile fp = do+ contents <- liftIO $ handle+ (\e -> return $ Left (e :: IOException))+ (liftM Right $ Prelude.readFile fp)+ case contents of+ Left e -> failure e+ Right v -> return v #ifdef CME
safe-failure.cabal view
@@ -1,7 +1,7 @@ Name: safe-failure Build-Type: Simple Cabal-Version: >= 1.2-Version: 0.1+Version: 0.2 License: BSD3 License-File: LICENSE Copyright: 2007-8, Neil Mitchell@@ -9,7 +9,7 @@ Author: Neil Mitchell, Jose Iborra (2009), Michael Snoyman (2009) Homepage: http://www-users.cs.york.ac.uk/~ndm/safe/ Category: Unclassified-Synopsis: Library for safe functions+Synopsis: Partial functions from the prelude with a MonadFailure interface Description: Partial functions from the base library, such as @head@ and @!!@, modified to fail in a @MonadFailure@ monad.@@ -21,20 +21,9 @@ description: build with special support for control-monad-exception default: True -Flag extensibleExceptions- description: Use extensible-exception package- default: False- Library buildable: True- build-depends: control-monad-failure- if flag(extensibleExceptions)- build-depends:- extensible-exceptions >= 0.1 && <0.2,- base >= 3.0 && <4- else- build-depends:- base >= 4 && < 5+ build-depends: base >= 4 && < 5, control-monad-failure, transformers exposed-modules: Safe.Failure ghc-options: -Wall