on master help sign in
Hoist out duplicated catchError definitions :-(
be28fb7Bryan O'Sullivan
Data/Text/IO.hs+12−17
@@ -98,20 +98,22 @@ writeFile p = withFile p WriteMode . flip hPutStr
98appendFile :: FilePath -> Text -> IO ()
98appendFile :: FilePath -> Text -> IO ()
99appendFile p = withFile p AppendMode . flip hPutStr
99appendFile p = withFile p AppendMode . flip hPutStr
100
100
101+catchError :: String -> Handle -> Handle__ -> IOError -> IO Text
102+catchError caller h Handle__{..} err
103+ | isEOFError err = do
104+ buf <- readIORef haCharBuffer
105+ return $ if isEmptyBuffer buf
106+ then T.empty
107+ else T.singleton '\r'
108+ | otherwise = E.throwIO (augmentIOError err caller h)
109+
101-- | Read a single chunk of strict text from a 'Handle'.
110-- | Read a single chunk of strict text from a 'Handle'.
102hGetChunk :: Handle -> IO Text
111hGetChunk :: Handle -> IO Text
103hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
112hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
104 where
113 where
105 readSingleChunk hh@Handle__{..} = do
114 readSingleChunk hh@Handle__{..} = do
106− let catchError e
107− | isEOFError e = do
108− buf <- readIORef haCharBuffer
109− return $ if isEmptyBuffer buf
110− then T.empty
111− else T.singleton '\r'
112− | otherwise = throwIO (augmentIOError e "hGetChunk" h)
113 buf <- readIORef haCharBuffer
115 buf <- readIORef haCharBuffer
114− t <- readChunk hh buf `catch` catchError
116+ t <- readChunk hh buf `E.catch` catchError "hGetChunk" h hh
115 return (hh, t)
117 return (hh, t)
116
118
117-- | Read the remaining contents of a 'Handle' as a string. The
119-- | Read the remaining contents of a 'Handle' as a string. The
@@ -134,16 +136,9 @@ hGetContents h = do
134 wantReadableHandle "hGetContents" h readAll
136 wantReadableHandle "hGetContents" h readAll
135 where
137 where
136 readAll hh@Handle__{..} = do
138 readAll hh@Handle__{..} = do
137− let catchError e
138− | isEOFError e = do
139− buf <- readIORef haCharBuffer
140− return $ if isEmptyBuffer buf
141− then T.empty
142− else T.singleton '\r'
143− | otherwise = E.throwIO (augmentIOError e "hGetContents" h)
144− readChunks = do
139+ let readChunks = do
145 buf <- readIORef haCharBuffer
140 buf <- readIORef haCharBuffer
146− t <- readChunk hh buf `E.catch` catchError
141+ t <- readChunk hh buf `E.catch` catchError "hGetContents" h hh
147 if T.null t
142 if T.null t
148 then return [t]
143 then return [t]
149 else (t:) `fmap` readChunks
144 else (t:) `fmap` readChunks