Skip to content

Commit

Permalink
Merge pull request #1236 from input-output-hk/fix-with-file
Browse files Browse the repository at this point in the history
Add fixed version of withFile to hydra-prelude
  • Loading branch information
ch1bo authored Jan 5, 2024
2 parents 0d14f10 + f3ac11a commit bd8a113
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions hydra-prelude/src/Hydra/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Hydra.Prelude (
Except,
decodeBase16,
(?>),
withFile,
) where

import Cardano.Binary (
Expand All @@ -44,6 +45,7 @@ import Control.Concurrent.Class.MonadSTM.TBQueue (TBQueue)
import Control.Concurrent.Class.MonadSTM.TMVar (TMVar)
import Control.Concurrent.Class.MonadSTM.TQueue (TQueue)
import Control.Concurrent.Class.MonadSTM.TVar (TVar, readTVar)
import Control.Exception (IOException)
import Control.Monad.Class.MonadAsync (
MonadAsync (concurrently, concurrently_, race, race_, withAsync),
)
Expand Down Expand Up @@ -134,6 +136,7 @@ import Relude hiding (
tryReadTMVar,
tryTakeMVar,
tryTakeTMVar,
withFile,
writeTVar,
)
import Relude.Extra.Map (
Expand All @@ -142,6 +145,7 @@ import Relude.Extra.Map (
elems,
keys,
)
import System.IO qualified
import Test.QuickCheck (
Arbitrary (..),
Gen,
Expand Down Expand Up @@ -235,3 +239,13 @@ infixl 4 ?>
case m of
Nothing -> Left e
Just a -> Right a

-- | Like 'withFile' from 'base', but without annotating errors originating from
-- enclosed action.
--
-- XXX: This should be fixed upstream in 'base'.
withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a
withFile fp mode action =
System.IO.withFile fp mode (try . action) >>= \case
Left (e :: IOException) -> throwIO e
Right x -> pure x

0 comments on commit bd8a113

Please sign in to comment.