packages feed

error-or 0.1.0.0 → 0.1.1.0

raw patch · 3 files changed

+38/−7 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Data.ErrorOr: tagIO :: Text -> IO a -> IO a

Files

+ changelog.md view
@@ -0,0 +1,3 @@+## error-or 0.1.1.0 (2020-12-02)++  * Add `tagIO`
error-or.cabal view
@@ -4,14 +4,14 @@ -- -- see: https://github.com/sol/hpack ----- hash: adafc3695349d4a839e4306d98388d9585cf81d5f436fe7d375b4a8a721c3518+-- hash: 06c1fad6bdd82d63e1afd222435d09bd98950ec171c0650e08d3af410ad47f34  name:           error-or-version:        0.1.0.0+version:        0.1.1.0 synopsis:       Composable, hierarchical errors. description:    An Applicative Functor with error accumulation. Built in tree like error type meant for gathering and reporting errors to users. category:       data,library-homepage:       https://github.com/luntain/error-or#readme+homepage:       https://github.com/luntain/error-or-bundle/blob/master/error-or#readme author:         Kamil Dworakowski maintainer:     kamil@dworakowski.name copyright:      2020 Kamil Dworakowski@@ -21,6 +21,7 @@ build-type:     Simple extra-source-files:     README.md+    changelog.md  library   exposed-modules:
src/Data/ErrorOr.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveTraversable #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}@@ -28,17 +29,18 @@     ErrorAcc(..),     pretty,     PrettyErrAcc (..),+    tagIO,   ) where  import qualified Control.Exception as Exc-import Control.Monad.IO.Class import Data.Foldable (toList)-import Data.Semigroup import qualified Data.Sequence as Seq import qualified Data.Text as T+import GHC.IO.Exception (IOException) #if __GLASGOW_HASKELL__ < 880 import Prelude hiding (fail)+import Data.Semigroup import Control.Monad.Fail (MonadFail(..)) #endif @@ -58,7 +60,7 @@   | Tag T.Text ErrorAcc   deriving (Show, Read, Eq, Ord) --- | Could be defined as 'err = fail . Text.pack'+-- | Produce an error. err :: T.Text -> ErrorOr a err = ErrorOr . Left . Message @@ -78,6 +80,31 @@  instance Exc.Exception PrettyErrAcc where +-- | Tag an exception with an annotation.+--+-- It acts on two types of exceptions: `IOException` and+-- `PrettyErrAcc`. For `PrettyErrAcc` it is streightforward tagging.+-- For `IOException`, otoh, it converts the error message into Text+-- via String and turns it into `PrettyErrAcc` tagged with provided+-- adnotation.+--+-- This is rather a convenience function. Sometimes it is convenient+-- to @fail "msg"@ in IO, and tag it higher up with some context. The+-- need for 'tagIO' often comes with 'Data.ErrorOr.Utils.lookup' (from+-- error-or-utils package) when used from IO, which is overloaded for+-- MonadFail.+--+-- Since ver 0.1.1.0+tagIO :: T.Text -> IO a -> IO a+tagIO str action =+  (action+     `Exc.catch` \(e :: IOException) ->+         Exc.throwIO $ PrettyErrAcc $ Tag str (Message (T.pack $ show e)))++     `Exc.catch` \(e :: PrettyErrAcc) ->+         Exc.throwIO $ PrettyErrAcc $ Tag str (unPrettyErrAcc e)++ -- | Pretty print the error. pretty :: Int   -- ^ Initial indent, usually 0@@ -142,7 +169,7 @@ mapError f (ErrorOr (Left e)) = ErrorOr (Left (f e)) mapError _ ok = ok --- | Like 'fromRight'+-- | A partial function, like 'fromRight'. fromOK :: ErrorOr a -> a fromOK (OK a) = a fromOK (Error err) = error (T.unpack $ pretty 0 err)