error-or 0.1.2.0 → 0.2.0.0
raw patch · 3 files changed
+60/−29 lines, 3 filesdep +mtlPVP ok
version bump matches the API change (PVP)
Dependencies added: mtl
API changes (from Hackage documentation)
- Data.ErrorOr: List :: Seq ErrorAcc -> ErrorAcc
- Data.ErrorOr: Message :: Text -> ErrorAcc
- Data.ErrorOr: Tag :: Text -> ErrorAcc -> ErrorAcc
- Data.ErrorOr: err :: Text -> ErrorOr a
- Data.ErrorOr: pretty :: Int -> ErrorAcc -> Text
+ Data.ErrorOr: ErrList :: Seq ErrorAcc -> ErrorAcc
+ Data.ErrorOr: ErrMessage :: Text -> ErrorAcc
+ Data.ErrorOr: ErrTag :: Text -> ErrorAcc -> ErrorAcc
+ Data.ErrorOr: failText :: Text -> ErrorOr a
+ Data.ErrorOr: instance Control.Monad.Error.Class.MonadError Data.ErrorOr.ErrorAcc Data.ErrorOr.ErrorOr
+ Data.ErrorOr: instance Data.String.IsString Data.ErrorOr.ErrorAcc
+ Data.ErrorOr: instance GHC.Show.Show e => Data.ErrorOr.ErrorConv (Data.Either.Either e) Data.ErrorOr.ErrorOr
+ Data.ErrorOr: prettyErrAcc :: Int -> ErrorAcc -> Text
Files
- changelog.md +12/−1
- error-or.cabal +6/−4
- src/Data/ErrorOr.hs +42/−24
changelog.md view
@@ -1,3 +1,14 @@-## error-or 0.1.1.0 (2020-12-02)+## 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`+ - prepend `ErrAcc` constructors with 'Err' prefix+ - rename `pretty` to `prettyErrAcc`+ * Define a `MonadError` instance for `ErrorOr`++## 0.1.2.0 (2020-12-16)++ * Mark the `OK` and `Error` patterns of `ErrorOr` complete for the exhaustiveness checker++## 0.1.1.0 (2020-12-02) * Add `tagIO`
error-or.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.34.4. -- -- see: https://github.com/sol/hpack ----- hash: 92aaa36d0bc66d07884e22009595e7dc11ed89e187d8a44f37bc13a0cb537f0f+-- hash: b491cc72be2df57b2360957ec79bc6e8d9b7d5e8e437fcc9174600d492fcd4b4 name: error-or-version: 0.1.2.0+version: 0.2.0.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@@ -17,7 +17,8 @@ copyright: 2020 Kamil Dworakowski 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+tested-with:+ GHC==8.0.1, GHC==8.2.2, GHC==8.8.1, GHC==8.8.3, GHC==8.10.2 build-type: Simple extra-source-files: README.md@@ -33,5 +34,6 @@ build-depends: base >=4.9 && <5 , containers+ , mtl , text default-language: Haskell2010
src/Data/ErrorOr.hs view
@@ -13,21 +13,21 @@ -- | Provides composable and hierarchical errors, with pretty -- printing. The errors are accumulated in a tree like structure, -- 'ErrorAcc'. 'ErrorAcc' is disigned to be read by humans, via--- 'pretty', not dispatched on by code. Using 'toE' to convert an+-- '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. module Data.ErrorOr ( ErrorOr(..),- err, tag, pattern Error, pattern OK, isOK, isError, fromOK,+ failText, ErrorConv(..), ErrorAcc(..),- pretty,+ prettyErrAcc, PrettyErrAcc (..), tagIO, )@@ -43,6 +43,8 @@ import Data.Semigroup import Control.Monad.Fail (MonadFail(..)) #endif+import Control.Monad.Except hiding (fail) -- 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'. newtype ErrorOr a = ErrorOr {errorOrToEither :: Either ErrorAcc a}@@ -52,25 +54,28 @@ pattern OK x <- ErrorOr (Right x) pattern Error :: ErrorAcc -> ErrorOr a-pattern Error err <- ErrorOr (Left err)+pattern Error err = ErrorOr (Left err) {-# COMPLETE OK, Error #-} data ErrorAcc- = Message T.Text- | List (Seq.Seq ErrorAcc)- | Tag T.Text ErrorAcc+ = ErrMessage T.Text+ | ErrList (Seq.Seq ErrorAcc)+ | ErrTag T.Text ErrorAcc deriving (Show, Read, Eq, Ord) --- | Produce an error.-err :: T.Text -> ErrorOr a-err = ErrorOr . Left . Message+instance IsString ErrorAcc where+ fromString = ErrMessage . T.pack +-- | Produce an error from `Text`. You can also use `fail`, which takes a `String`.+failText :: T.Text -> ErrorOr a+failText = ErrorOr . Left . ErrMessage+ -- | Annotate the error with context information. tag :: T.Text -> ErrorOr a -> ErrorOr a tag str res | isOK res = res- | otherwise = mapError (Tag str) res+ | otherwise = mapError (ErrTag str) res -- | A wrapper over 'ErrorAcc' to provide human readable exceptions. -- (Exception class' displayException does not seem to be used by GHC)@@ -78,7 +83,7 @@ newtype PrettyErrAcc = PrettyErrAcc {unPrettyErrAcc :: ErrorAcc} instance Show PrettyErrAcc where- show = T.unpack . pretty 0 . unPrettyErrAcc+ show = T.unpack . prettyErrAcc 0 . unPrettyErrAcc instance Exc.Exception PrettyErrAcc where @@ -101,25 +106,25 @@ tagIO str action = (action `Exc.catch` \(e :: IOException) ->- Exc.throwIO $ PrettyErrAcc $ Tag str (Message (T.pack $ show e)))+ Exc.throwIO $ PrettyErrAcc $ ErrTag str (ErrMessage (T.pack $ show e))) `Exc.catch` \(e :: PrettyErrAcc) ->- Exc.throwIO $ PrettyErrAcc $ Tag str (unPrettyErrAcc e)+ Exc.throwIO $ PrettyErrAcc $ ErrTag str (unPrettyErrAcc e) -- | Pretty print the error.-pretty :: Int+prettyErrAcc :: Int -- ^ Initial indent, usually 0 -> ErrorAcc -> T.Text-pretty indent (Message txt) = T.replicate indent " " <> txt-pretty indent (List errs) = T.intercalate "\n" . map (pretty indent) . toList $ errs-pretty indent (Tag str err) = T.intercalate "\n" [pretty indent (Message str), pretty (indent + 4) err]+prettyErrAcc indent (ErrMessage txt) = T.replicate indent " " <> txt+prettyErrAcc indent (ErrList errs) = T.intercalate "\n" . map (prettyErrAcc indent) . toList $ errs+prettyErrAcc indent (ErrTag str err) = T.intercalate "\n" [prettyErrAcc indent (ErrMessage str), prettyErrAcc (indent + 4) err] instance Semigroup ErrorAcc where- List l1 <> List l2 = List (l1 <> l2)- List l1 <> other = List (l1 Seq.|> other)- other <> List l2 = List (other Seq.<| l2)- notList1 <> notList2 = List (Seq.fromList [notList1, notList2])+ ErrList l1 <> ErrList l2 = ErrList (l1 <> l2)+ ErrList l1 <> other = ErrList (l1 Seq.|> other)+ other <> ErrList l2 = ErrList (other Seq.<| l2)+ notList1 <> notList2 = ErrList (Seq.fromList [notList1, notList2]) instance Applicative ErrorOr where pure x = ErrorOr (Right x)@@ -158,7 +163,7 @@ ErrorOr either >>= f = ErrorOr (either >>= fmap errorOrToEither f) instance MonadFail ErrorOr where- fail = ErrorOr . Left . Message . T.pack+ fail = ErrorOr . Left . ErrMessage . T.pack isOK :: ErrorOr a -> Bool isOK (OK _) = True@@ -174,8 +179,16 @@ -- | A partial function, like 'fromRight'. fromOK :: ErrorOr a -> a fromOK (OK a) = a-fromOK (Error err) = error (T.unpack $ pretty 0 err)+fromOK (Error err) = error (T.unpack $ prettyErrAcc 0 err) +instance MonadError ErrorAcc ErrorOr where+ -- | Usually it is more convenient to use `fail` than this method.+ throwError err = ErrorOr (Left err)+ catchError action handler =+ case action of+ OK a -> action+ Error err -> handler err+ -- | Convert between functors that hold error info. class ErrorConv t s where toE :: t a -> s a@@ -190,3 +203,8 @@ instance ErrorConv Maybe ErrorOr where toE Nothing = fail "Nothing" toE (Just a) = pure a++-- | Convert from `Either e`, `Left` becomese error.+instance Show e => ErrorConv (Either e) ErrorOr where+ toE (Left err) = fail (show err)+ toE (Right a) = pure a