From 13819af69d4a99f97eaf8ae439baeb6de37a1f5f Mon Sep 17 00:00:00 2001 From: Sam T Date: Fri, 7 Jun 2013 19:14:41 +0400 Subject: ~ Throw exception in askTracker. HTTP client functions throws an exception. If we unable to decode result we return BEncode.Result.Left. So user of this function should check both kinds of errors and this complicate usage. Instead of this we throw IOException too. --- src/Network/BitTorrent/Tracker/Protocol.hs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/Network/BitTorrent/Tracker/Protocol.hs') diff --git a/src/Network/BitTorrent/Tracker/Protocol.hs b/src/Network/BitTorrent/Tracker/Protocol.hs index 0371a187..af48e3e9 100644 --- a/src/Network/BitTorrent/Tracker/Protocol.hs +++ b/src/Network/BitTorrent/Tracker/Protocol.hs @@ -24,7 +24,7 @@ module Network.BitTorrent.Tracker.Protocol ( module Network.BitTorrent.Tracker.Scrape , Event(..), TRequest(..), TResponse(..) - , sendRequest + , askTracker -- * Defaults , defaultPorts, defaultNumWant @@ -220,16 +220,21 @@ defaultPorts = [6881..6889] defaultNumWant :: Int defaultNumWant = 25 --- | TODO rename to ask for peers +-- | Send request and receive response from the tracker specified in +-- announce list. This function throws 'IOException' if it couldn't +-- send request or receive response or decode response. -- -sendRequest :: TRequest -> IO (Result TResponse) -sendRequest req = do - let r = mkHTTPRequest (encodeRequest req) - - rawResp <- simpleHTTP r - respBody <- getResponseBody rawResp - return (decoded respBody) +askTracker :: TRequest -> IO TResponse +askTracker req = do + let r = mkHTTPRequest (encodeRequest req) + rawResp <- simpleHTTP r + respBody <- getResponseBody rawResp + checkResult $ decoded respBody where mkHTTPRequest :: URI -> Request ByteString mkHTTPRequest uri = Request uri GET [] "" + + checkResult (Left err) = ioError (userError err) + checkResult (Right (Failure err)) = ioError (userError (show err)) + checkResult (Right resp) = return resp -- cgit v1.2.3