Skip to content

Commit

Permalink
decodeFileEither: propagate IOExceptions correctly
Browse files Browse the repository at this point in the history
The interface we wrap expects only Yaml.ParseException to be thrown, and
to be wrapped inside Either. Conform to that.
  • Loading branch information
Blaisorblade committed Aug 14, 2016
1 parent a0c30b7 commit e217cfb
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Data/Yaml/Extra.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
{-# LANGUAGE ScopedTypeVariables #-}
-- | Wrappers for Yaml functions to workaround
-- https://github.com/commercialhaskell/stack/issues/2491.
-- Import Data.Yaml.Extra in place of Data.Yaml to use this workaround.
-- Beware these functions construct/deconstruct the entire file at once!
module Data.Yaml.Extra (decodeFileEither, encodeFile, module Data.Yaml) where

import Control.Exception
import Data.Yaml hiding (decodeFileEither, encodeFile)
import qualified Data.ByteString as B
import System.IO

-- Note: we refrain from using 'B.readFile' and 'B.writeFile', as they open
-- the file in binary mode rather than text mode.
decodeFileEither :: FromJSON a => FilePath -> IO (Either ParseException a)
decodeFileEither path = withFile path ReadMode $
decodeFileEither path =
handle (\(e :: IOException) -> return . Left . OtherParseException . SomeException $ e) $
withFile path ReadMode $
\hnd -> do
fileContent <- B.hGetContents hnd
return $ decodeEither' fileContent
Expand Down

0 comments on commit e217cfb

Please sign in to comment.