Skip to content

Commit

Permalink
Handle HTTP exceptions when fetching data.
Browse files Browse the repository at this point in the history
The old libcurl bindings returned an empty string when a page could not
be found (HTTP 404). On the other hand, Network.HTTP.Conduit throws
exceptions on non-2xx status codes so those need to be handled so they
don't get propagated.
  • Loading branch information
Denis Kasak committed Feb 17, 2014
1 parent 9b2f889 commit 45652a2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Internet.hs
Expand Up @@ -29,7 +29,8 @@ module Internet

import qualified Data.ByteString.Lazy as L

import Data.ByteString.Lazy.Char8 (unpack)
import Data.ByteString.Lazy.Char8 (pack, unpack)
import Control.Exception (catch)
import System.FilePath (splitFileName, (</>))
import Network.HTTP (urlEncodeVars)
import System.IO (hClose, openFile, IOMode(WriteMode))
Expand All @@ -41,7 +42,10 @@ urlContents :: String -> IO L.ByteString
urlContents url = do
req <- parseUrl url
let req2 = req { responseTimeout = Nothing}
responseBody `fmap` withManager (httpLbs req2)
catch (fmap responseBody . withManager . httpLbs $ req2) handleException

handleException :: HttpException -> IO L.ByteString
handleException _ = return (pack "")

saveUrlContents :: FilePath -> String -> IO FilePath
saveUrlContents fpath url = do
Expand Down

0 comments on commit 45652a2

Please sign in to comment.