2015-12-15 9 views
5

Wreqのgetメソッドを使用して404に実行すると、処理するstatusCodeではなく例外(下を参照)が表示されます。返されるのはわずか200秒です。Wreq:例外をスローする404sを停止します

tutorialのエラー処理コードに従ってみましたが、get uと同じタイプを返す方法が見つかりません。さらに、これは私がこのインスタンスで必要とする以上に複雑なようです。

どのように私は単に例外を防ぐことができ、ちょうどここで

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 [email protected](StatusCodeException s respHeaders _) = 
      do 
       return respHeaders 

スローされた例外で、あなたはそれが私がYurasが提案

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

をしたいstateCodeを持って見ることができているようresponseStatusを返しますオプションを使用していますが、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 

答えて

8

注:答えは古く、他の回答を参照してください。

は私がWreqを使用することはありませんが、あなたは、ステータスの処理を設定するには、カスタムオプションとcheckStatusを渡すためにgetWithを使用する必要があるように見えます。

例:

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

\_ _ _ -> NothingStatusCheckerを参照して、ステータス・コードをチェックする機能です。ステータスコードがOKであることを示すものは何も返しません。

+0

で動作するようにこれを得たが、問題はWreqがExceptionとして '200'以外のものを扱っているので、無効にしなければならないということです。 –

+0

@ SimonH私は例を追加しました – Yuras

+1

ありがとう、また中置演算子を使用しないことに感謝します –

2

ここではちょっと調べたところでcheckStatusという機能がありました。 HttpExceptionSomeExceptionに変換する方法はわかりませんでしたが、Control.Monad.Catch.SomeExceptionが見つかりました。これは404を無視し、他のすべての例外を再スローします。後世のために

import   Network.HTTP.Client.Types 
import   Network.HTTP.Types.Status 
import   Network.HTTP.Types.Header 
import qualified Control.Exception as E 
import   Control.Monad.Catch (SomeException(..)) 

notFoundMeansNothing :: Status -> ResponseHeaders -> CookieJar -> Maybe E.SomeException 
notFoundMeansNothing s h c 
    | s == status404 = Nothing 
    | otherwise = 
     if statusIsClientError s || statusIsServerError s then 
      Just . SomeException $ StatusCodeException s h c 
     else 
      Nothing 
2

:(0.5始まる)wreqの新しいバージョンは、異なる引数を取るcheckResponse、とcheckStatusを交換しました。 Yuras'と答えたと同等になりましたエヴリン・シュナイダーによって答えに展開する getWith opts url where opts = set checkResponse (\_ _ -> return()) defaults

2

だろう、私は私はあなたがここで意味のすべてを取得するのに十分経験していないよ

r <- getWith opts url 
where 
    opts = set Network.Wreq.checkResponse (Just $ \_ _ -> return()) defaults 
関連する問題