|
1 | 1 | {-# LANGUAGE ConstraintKinds #-} |
| 2 | +{-# LANGUAGE DefaultSignatures #-} |
2 | 3 | {-# LANGUAGE DeriveAnyClass #-} |
3 | 4 | {-# LANGUAGE ImpredicativeTypes #-} |
4 | 5 | {-# LANGUAGE LiberalTypeSynonyms #-} |
@@ -45,7 +46,6 @@ module Share.Postgres |
45 | 46 | defaultIsolationLevel, |
46 | 47 | pipelined, |
47 | 48 | pEitherMap, |
48 | | - pUnrecoverableEitherMap, |
49 | 49 | pFor, |
50 | 50 | pFor_, |
51 | 51 |
|
@@ -139,14 +139,6 @@ pEitherMap f (Pipeline p) = |
139 | 139 | Right x -> mapLeft Err (f x) |
140 | 140 | Left e -> Left e |
141 | 141 |
|
142 | | --- | Like 'pEitherMap', but for throwing unrecoverable errors. |
143 | | -pUnrecoverableEitherMap :: (Loggable e, Show e, ToServerError e) => (a -> Either e b) -> Pipeline e' a -> Pipeline e' b |
144 | | -pUnrecoverableEitherMap f (Pipeline p) = |
145 | | - Pipeline $ |
146 | | - p <&> \case |
147 | | - Right x -> mapLeft (Unrecoverable . someServerError) (f x) |
148 | | - Left e -> Left e |
149 | | - |
150 | 142 | pFor :: (Traversable f) => f a -> (a -> Pipeline e b) -> Transaction e (f b) |
151 | 143 | pFor f p = pipelined $ for f p |
152 | 144 |
|
@@ -346,6 +338,17 @@ class (Applicative m) => QueryA m where |
346 | 338 | -- | Fail the transaction and whole request with an unrecoverable server error. |
347 | 339 | unrecoverableError :: (HasCallStack, ToServerError e, Loggable e, Show e) => e -> m a |
348 | 340 |
|
| 341 | + -- | Map an either-returning function over the result of an action; if it returns Left, throw an unrecoverable error. |
| 342 | + -- This is a trivial combinator for any monad, hence the default signature, but it can be implemented by our |
| 343 | + -- Pipeline applicative, too. |
| 344 | + unrecoverableEitherMap :: (HasCallStack, Loggable e, Show e, ToServerError e) => (a -> Either e b) -> m a -> m b |
| 345 | + default unrecoverableEitherMap :: (HasCallStack, Loggable e, Show e, ToServerError e, Monad m) => (a -> Either e b) -> m a -> m b |
| 346 | + unrecoverableEitherMap f m = do |
| 347 | + x <- m |
| 348 | + case f x of |
| 349 | + Right y -> pure y |
| 350 | + Left e -> unrecoverableError e |
| 351 | + |
349 | 352 | class (Monad m, QueryA m) => QueryM m where |
350 | 353 | -- | Allow running IO actions in a transaction. These actions may be run multiple times if |
351 | 354 | -- the transaction is retried. |
@@ -376,6 +379,12 @@ instance QueryA (Pipeline e) where |
376 | 379 |
|
377 | 380 | unrecoverableError e = Pipeline $ pure (Left (Unrecoverable (someServerError e))) |
378 | 381 |
|
| 382 | + unrecoverableEitherMap f (Pipeline p) = |
| 383 | + Pipeline $ |
| 384 | + p <&> \case |
| 385 | + Right x -> mapLeft (Unrecoverable . someServerError) (f x) |
| 386 | + Left e -> Left e |
| 387 | + |
379 | 388 | instance (QueryM m) => QueryA (ReaderT e m) where |
380 | 389 | statement q s = lift $ statement q s |
381 | 390 |
|
|
0 commit comments