-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathError.hs
57 lines (44 loc) · 1.35 KB
/
Error.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
module RON.Error (
Error (..),
MonadE,
correct,
errorContext,
liftEither,
liftEitherString,
liftMaybe,
throwErrorString,
throwErrorText,
) where
import RON.Prelude
import Data.String (IsString, fromString)
data Error = Error Text [Error]
deriving (Eq, Show)
{- TODO(2019-08-09, cblp)
data Error = Error
{ context :: [Text]
, desc :: Text
, reasons :: [Error]
}
-}
instance Exception Error
instance IsString Error where
fromString s = Error (fromString s) []
type MonadE = MonadError Error
errorContext :: MonadE m => Text -> m a -> m a
errorContext ctx action = action `catchError` \e -> throwError $ Error ctx [e]
liftMaybe :: MonadE m => Text -> Maybe a -> m a
liftMaybe msg = maybe (throwErrorText msg) pure
liftEitherString :: (MonadError e m, IsString e) => Either String a -> m a
liftEitherString = either throwErrorString pure
throwErrorText :: MonadE m => Text -> m a
throwErrorText msg = throwError $ Error msg []
throwErrorString :: (MonadError e m, IsString e) => String -> m a
throwErrorString = throwError . fromString
correct :: MonadError e m => a -> m a -> m a
correct def action =
action
`catchError` \_e ->
-- TODO(2019-08-06, cblp) $logWarnSH e
pure def