Wreq:停止404s抛出异常

时间:2015-12-15 09:28:51

标签: haskell

我正在尝试测试损坏的链接但是,当我使用Wreq的get方法并运行到404时,我得到一个异常(见底部)而不是要处理的statusCode。似乎只有200多人返回。

我尝试按照tutorial中的错误处理代码,但我找不到与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

这是抛出的异常,你可以看到它有我想要的stateCode

*Main> re <- get "https://www.seatme.nl/restaurant/1371/Londen.htm"
*** Exception: StatusCodeException (Status {statusCode = 404, statusMessage = "Not Found"}) [("Cache-Control","private"),....

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

4 个答案:

答案 0 :(得分:7)

注意:答案已过时,请参阅其他答案。

我从未使用Wreq,但看起来您应该使用getWith来传递自定义选项,checkStatus来配置状态处理。

一个例子:

getWith (set checkStatus (Just $ \_ _ _ -> Nothing) defaults)
  "http://google.com/hello/world"

\_ _ _ -> Nothing是检查状态代码的功能,请参阅StatusChecker。它不返回任何状态代码正常的信息。

答案 1 :(得分:5)

To expand on the answer by Evelyn Schneider, I got this to work with

r <- getWith opts url
where 
  opts = set Network.Wreq.checkResponse (Just $ \_ _ -> return ()) defaults

答案 2 :(得分:4)

对于后代:较新版本的checkStatus(以checkResponse开头)已将getWith opts url where opts = set checkResponse (\_ _ -> return ()) defaults 替换为ajax,后者采用不同的参数。相当于Yuras&#39;答案现在是:

wp-ajax.php

答案 3 :(得分:2)

这是我研究了一下之后最终得到的var isChecked: Bool = false { didSet{ if isChecked == true { self.image = checkedImage }else{ self.image = unCheckedImage } } } 函数。我无法弄清楚如何将checkStatus转换为HttpException,但之后我找到了SomeException。这将忽略404并重新抛出所有其他异常。

Control.Monad.Catch.SomeException