我试图测试断开的链接但是,当我使用Wreq的get方法并运行到404时,我得到一个异常(见底部)而不是要处理的statusCode.似乎只返回了200s.
我试图按照教程中的错误处理代码,但我找不到返回相同类型的方法get u.而且,这似乎比我在这种情况下需要的更复杂.
我怎样才能简单地阻止异常并按原样返回responseStatus
verifySeatme :: Maybe URL -> IO UrlStatus
verifySeatme url = do
case url of
Nothing -> return None
Just "" -> return None
Just u -> do
seatmeResp <- get u --`E.catch` handler
-- r ^? responseBody . key "url"
-- could also check for redirect to errorPage.aspx
if seatmeResp ^. W.responseStatus . statusCode == 200
then return (Working u)
else return Broken
where
handler e@(StatusCodeException s respHeaders _) =
do
return respHeaders
Run Code Online (Sandbox Code Playgroud)
这是抛出的异常,你可以看到它有我想要的stateCode
*Main> re <- get "https://www.seatme.nl/restaurant/1371/Londen.htm"
*** Exception: StatusCodeException (Status {statusCode = 404, statusMessage = "Not Found"}) [("Cache-Control","private"),....
Run Code Online (Sandbox Code Playgroud)
Yuras建议使用选项,但我无法使用示例使用params到使用选项checkStatus :: Lens' Options (Maybe StatusChecker):
getData :: IO Restos
getData = do
let opts = defaults & customStatusHandler
jdata <- asJSON =<< getWith opts "http://localhost/restos-short.json" :: IO Resp
let
restos = jdata ^. W.responseBody
verified <- mapM processEntry restos
return verified
-- type StatusChecker = Status -> ResponseHeaders -> CookieJar -> Maybe SomeException
customStatusHandler :: W.StatusChecker
customStatusHandler st res _ =
Just res
Run Code Online (Sandbox Code Playgroud)
注意:答案已过时,请参阅其他答案.
我从未使用过Wreq,但看起来你应该使用getWith传递自定义选项和checkStatus来配置状态处理.
一个例子:
getWith (set checkStatus (Just $ \_ _ _ -> Nothing) defaults)
"http://google.com/hello/world"
Run Code Online (Sandbox Code Playgroud)
这\_ _ _ -> Nothing是一个检查状态代码的函数,请参阅StatusChecker.它不返回任何指示任何状态代码都正常的内容.
小智 5
对于后代:(以wreq开头0.5)的较新版本已替换checkStatus为checkResponse,它采用不同的参数。相当于 Yuras 的答案现在是:
getWith opts url
where opts = set checkResponse (\_ _ -> return ()) defaults
Run Code Online (Sandbox Code Playgroud)
小智 5
为了扩展Evelyn Schneider的答案,我得到了这个
r <- getWith opts url
where
opts = set Network.Wreq.checkResponse (Just $ \_ _ -> return ()) defaults
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1009 次 |
| 最近记录: |