summaryrefslogtreecommitdiff
path: root/src/Erebos/Error.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2025-03-11 20:22:33 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2025-03-21 20:15:21 +0100
commit83d291f476a9793012a7aabb27c3cf59c7bdea05 (patch)
tree42b60eac72c25df8280e412e706acbae80fa7a8b /src/Erebos/Error.hs
parentf612d03ac7d5fb00fa76c3be14d965ab51988504 (diff)
Generic type for MonadError constraints
Changelog: API: MonadError constraints use generic error type
Diffstat (limited to 'src/Erebos/Error.hs')
-rw-r--r--src/Erebos/Error.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Erebos/Error.hs b/src/Erebos/Error.hs
new file mode 100644
index 0000000..3bb8736
--- /dev/null
+++ b/src/Erebos/Error.hs
@@ -0,0 +1,39 @@
+module Erebos.Error (
+ ErebosError(..),
+ showErebosError,
+
+ FromErebosError(..),
+ throwOtherError,
+) where
+
+import Control.Monad.Except
+
+
+data ErebosError
+ = ManyErrors [ ErebosError ]
+ | OtherError String
+
+showErebosError :: ErebosError -> String
+showErebosError (ManyErrors errs) = unlines $ map showErebosError errs
+showErebosError (OtherError str) = str
+
+instance Semigroup ErebosError where
+ ManyErrors [] <> b = b
+ a <> ManyErrors [] = a
+ ManyErrors a <> ManyErrors b = ManyErrors (a ++ b)
+ ManyErrors a <> b = ManyErrors (a ++ [ b ])
+ a <> ManyErrors b = ManyErrors (a : b)
+ a@OtherError {} <> b@OtherError {} = ManyErrors [ a, b ]
+
+instance Monoid ErebosError where
+ mempty = ManyErrors []
+
+
+class FromErebosError e where
+ fromErebosError :: ErebosError -> e
+
+instance FromErebosError ErebosError where
+ fromErebosError = id
+
+throwOtherError :: (MonadError e m, FromErebosError e) => String -> m a
+throwOtherError = throwError . fromErebosError . OtherError