【发布时间】:2017-01-16 22:48:29
【问题描述】:
我正在实现如下功能: 用户将通过 HTTP 请求请求目录内容。目录路径将在请求中传递。作为响应,我将发送一个 JSON,其中包含该目录路径的文件列表。处理 IO 错误处理。 我已经实现了大部分。但我收到一个错误。错误是:
Couldn't match expected type ‘Either IOException [FilePath]’ with actual type ‘IO (Either IOException [FilePath])’ • In the first argument of ‘getJsonRepForFiles’, namely ‘files’
sn-p代码如下:
getInfoOfDirR::Handler Value
getInfoOfDirR = do
-- files will have type IO (Either IOException [FilePath])
files <- fmap (getListOfFiles.(Data.Text.unpack).fromJust) $ lookupGetParam "dirpath"
getJsonRepForFiles files
获取文件列表的函数:
-- This eliminates the hidden files from the results
getListOfFiles::FilePath -> IO (Either IOException [FilePath])
getListOfFiles fpath = try (fmap (filter $ not.isHiddenFile) $ FS.getDirectoryContents fpath)::IO (Either IOException [FilePath])
获取目录中文件的 JSON 表示的函数:
getJsonRepForFiles::Monad m => (Either IOException [FilePath]) -> m Value
getJsonRepForFiles (Left e) = returnJson $ "An error occurred" ++ show e
getJsonRepForFiles (Right files) = returnJson files
注意getInfoOfDirR 的类型是Handler Value
我在 Haskell 的 Yesod 框架中实现了这一点。
我是 Haskell 的新手。我有点理解为什么会出现错误。
但我无法修复它。
请帮忙。
提前致谢。
【问题讨论】:
-
请注意,我尝试过使用
liftIO,但也没有解决问题