module Unix.Errors
    ( retryEINTR
    , throwIfErrno
    ) where

import Zhp

import Control.Exception (throwIO)
import Foreign.C.Error

throwIfErrno :: IO (Either Errno a) -> IO a
throwIfErrno :: IO (Either Errno a) -> IO a
throwIfErrno IO (Either Errno a)
io = do
    Either Errno a
r <- IO (Either Errno a)
io
    case Either Errno a
r of
        Left Errno
e  -> IOError -> IO a
forall e a. Exception e => e -> IO a
throwIO (IOError -> IO a) -> IOError -> IO a
forall a b. (a -> b) -> a -> b
$ String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
"" Errno
e Maybe Handle
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
        Right a
v -> a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v

retryEINTR :: IO (Either Errno a) -> IO (Either Errno a)
retryEINTR :: IO (Either Errno a) -> IO (Either Errno a)
retryEINTR IO (Either Errno a)
io = do
    Either Errno a
r <- IO (Either Errno a)
io
    case Either Errno a
r of
        Left Errno
e | Errno
e Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eINTR -> IO (Either Errno a) -> IO (Either Errno a)
forall a. IO (Either Errno a) -> IO (Either Errno a)
retryEINTR IO (Either Errno a)
io
        Either Errno a
_                   -> Either Errno a -> IO (Either Errno a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either Errno a
r