In the following:
import Data.Bifunctor
import qualified Data.ByteString.Lazy.UTF8 as BLU
safeReadFile :: FilePath -> ExceptT Text IO Text
safeReadFile p = (lift $ doesFileExist p) >>= bool (throwError "File does not exist") (lift $ pack <$> readFile p)
safeDecodeJSONFile :: FromJSON a => Text -> FilePath -> ExceptT Text IO a
safeDecodeJSONFile t f = do
contents <- safeReadFile f
tryRight $ first (\x -> pack (x ++ (unpack t))) (eitherDecode (BLU.fromString (unpack contents)))
When I run runExceptT $ safeDecodeJSONFile "something" "nonExistantFile.json"
I expect to get Left "Does not exist something"
but instead I just get Left "Does not exist"
- I know that the function I pass to first
is being executed, since without the pack
GHC complains that the type of (eitherDecode (BLU.fromString (unpack contents)))
is ExceptT String IO a
instead of ExceptT Text IO a
- so why doesn't the concatenation from ++
also happen?