<commit_before>import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Control.Exception as X

badUrl = "http://www.google.com/intl/en_com/images/srpr/WRONG.png"    

oldmain = do
    imgData <- (simpleHttp badUrl) `X.catch` statusExceptionHandler  
    case imgData of x | x == L.empty -> return () 
                      | otherwise    -> L.writeFile "my.png" imgData

    where
        statusExceptionHandler ::  HttpException -> IO L.ByteString
        statusExceptionHandler (StatusCodeException status headers) = 
            putStr "An error occured during download: "
            >> (putStrLn $ show status)
            >> (return L.empty)

main = do
    let imgData = getImgData
    case imgData of  Just x  -> x >>= L.writeFile "test.png"
                     Nothing -> return ()
    where
        getImgData :: Maybe (IO L.ByteString)
        getImgData =  (Just $ simpleHttp badUrl) -- `X.catch` statusExceptionHandler 

        statusExceptionHandler :: HttpException -> Maybe (IO L.ByteString)
        statusExceptionHandler (StatusCodeException status headers) = Nothing
    

<commit_msg>implemented exception handling using try<commit_after>import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Control.Exception as X

badUrl = "http://www.google.com/intl/en_com/images/srpr/WRONG.png"    

main = usingTry

xcatchReturningEmptyOnError = do
    imgData <- (simpleHttp badUrl) `X.catch` statusExceptionHandler  
    case imgData of x | x == L.empty -> return () 
                      | otherwise    -> L.writeFile "my.png" imgData

    where
        statusExceptionHandler ::  HttpException -> IO L.ByteString
        statusExceptionHandler (StatusCodeException status headers) = 
            putStr "An error occured during download: "
            >> (putStrLn $ show status)
            >> (return L.empty)

usingTry = do
    imgData <- try (simpleHttp badUrl)
    case imgData of  
        Right byteString                           -> L.writeFile "test.png" byteString
        Left  (StatusCodeException status headers) -> 
            putStr "An error occured during download: "
            >> (putStrLn $ show status)
{'fraction_non_alphanumeric': 0.08916323731138547, 'fraction_numerical': 0.0, 'mean_word_length': 2.376543209876543, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2526656', 'n_tokens_mistral': 610, 'n_tokens_neox': 598, 'n_words': 171}