在下面的:
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)))
当我运行时,runExceptT $ safeDecodeJSONFile "something" "nonExistantFile.json"
我希望得到Left "Does not exist something"
,但我只是得到Left "Does not exist"
- 我知道我传递给的函数first
正在执行,因为没有pack
GHC 抱怨类型(eitherDecode (BLU.fromString (unpack contents)))
是ExceptT String IO a
而不是ExceptT Text IO a
- 那么为什么连接++
也不会发生呢?