error-or 0.2.0.0 → 0.3.0
raw patch · 4 files changed
+36/−28 lines, 4 filesdep ~mtlPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: mtl
API changes (from Hackage documentation)
- Data.ErrorOr: instance (GHC.Base.Semigroup (Data.ErrorOr.ErrorOr a), GHC.Base.Monoid a) => GHC.Base.Monoid (Data.ErrorOr.ErrorOr a)
+ Data.ErrorOr: instance GHC.Base.Monoid a => GHC.Base.Monoid (Data.ErrorOr.ErrorOr a)
Files
- README.md +1/−1
- changelog.md +4/−0
- error-or.cabal +4/−3
- src/Data/ErrorOr.hs +27/−24
README.md view
@@ -1,6 +1,6 @@ # error-or -[](https://travis-ci.com/luntain/error-or)+[](https://hackage.haskell.org/package/error-or) Composable, hierarchical errors.
changelog.md view
@@ -1,3 +1,7 @@+## 0.3.0 (2022-06-16)+ * tagIO now wraps any non async exception, used to wrap only `IOExceptions` and+ user errors (from `error` function)+ * compatiblity with mtl-2.3 ## 0.2.0.0 (2021-06-24) * Renames to reduce the likelihood of name collisions on importing the module unqualified - rename `err` to `failText`, to suggest the use of `MonadFail.fail`
error-or.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: b491cc72be2df57b2360957ec79bc6e8d9b7d5e8e437fcc9174600d492fcd4b4+-- hash: 941c3da601f16a0d99d2f6eebbe072e40ca00ea145b28ff7cf1057712fe945a5 name: error-or-version: 0.2.0.0+version: 0.3.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@@ -18,7 +18,7 @@ license: BSD3 license-file: LICENSE tested-with:- GHC==8.0.1, GHC==8.2.2, GHC==8.8.1, GHC==8.8.3, GHC==8.10.2+ GHC == 8.0.2 || ==8.2.2 || ==8.6.5 || ==8.8.3 || ==8.10.2 || ==9.0.1 || ==9.2.1 build-type: Simple extra-source-files: README.md@@ -31,6 +31,7 @@ Paths_error_or hs-source-dirs: src+ ghc-options: -Wall -fno-warn-name-shadowing build-depends: base >=4.9 && <5 , containers
src/Data/ErrorOr.hs view
@@ -16,6 +16,8 @@ -- 'prettyErrAcc', not dispatched on by code. Using 'toE' to convert an -- 'ErrorOr' to IO throws (in case it holds an error) a 'PrettyErrAcc' -- that uses 'pretty' in the show instance.+--+-- This works well for apps where you don't need to display errors in different languages. module Data.ErrorOr ( ErrorOr(..), tag,@@ -37,13 +39,12 @@ import Data.Foldable (toList) import qualified Data.Sequence as Seq import qualified Data.Text as T-import GHC.IO.Exception (IOException)-#if __GLASGOW_HASKELL__ < 880+#if __GLASGOW_HASKELL__ < 808 import Prelude hiding (fail) import Data.Semigroup import Control.Monad.Fail (MonadFail(..)) #endif-import Control.Monad.Except hiding (fail) -- it exports GHC.Base.fail (?!)+import Control.Monad.Except (MonadError, throwError, catchError) -- it exports GHC.Base.fail (?!) import Data.String -- | Use 'Applicative'\'s 'sequenceA' and 'sequenceA_' to compose 'ErrorOr's as opposed to 'Monad' derived functions like 'sequence'.@@ -89,28 +90,30 @@ -- | 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.+-- It ignores asynchronous exceptions and has special logic for+-- `PrettyErrAcc` for better composition. For `SomeException`, it+-- converts the error message into Text via String and turns it into+-- `PrettyErrAcc` tagged with provided adnotation. For `PrettyErrAcc`+-- it is streightforward tagging. -- -- Since ver 0.1.1.0 tagIO :: T.Text -> IO a -> IO a-tagIO str action =- (action- `Exc.catch` \(e :: IOException) ->- Exc.throwIO $ PrettyErrAcc $ ErrTag str (ErrMessage (T.pack $ show e)))-- `Exc.catch` \(e :: PrettyErrAcc) ->- Exc.throwIO $ PrettyErrAcc $ ErrTag str (unPrettyErrAcc e)-+tagIO ctx action =+ action+ `Exc.catch` \(e :: Exc.SomeException) ->+ case Exc.fromException e of+ Just (pretty :: PrettyErrAcc) ->+ Exc.throwIO $ PrettyErrAcc $ ErrTag ctx (unPrettyErrAcc pretty)+ Nothing ->+ if isAsyncException e+ then Exc.throwIO $ PrettyErrAcc $ ErrTag ctx (ErrMessage (T.pack $ show e))+ else Exc.throwIO e+ where+ isAsyncException :: Exc.SomeException -> Bool+ isAsyncException e =+ case Exc.fromException e of+ Just (Exc.SomeAsyncException _) -> True+ Nothing -> False -- | Pretty print the error. prettyErrAcc :: Int@@ -140,7 +143,7 @@ _ <> r = r instance (-#if __GLASGOW_HASKELL__ < 880+#if __GLASGOW_HASKELL__ < 808 Semigroup (ErrorOr a), #endif Monoid a) => Monoid (ErrorOr a) where@@ -186,7 +189,7 @@ throwError err = ErrorOr (Left err) catchError action handler = case action of- OK a -> action+ OK _ -> action Error err -> handler err -- | Convert between functors that hold error info.