-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from fimad/error-handling-examples
Error handling examples
- Loading branch information
Showing
5 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import Text.HTML.Scalpel | ||
import Control.Applicative | ||
import Control.Monad.Writer.Class (tell) | ||
import Control.Monad.Writer.Strict (Writer, runWriter) | ||
|
||
|
||
exampleHtml :: String | ||
exampleHtml = "<html>\ | ||
\ <body>\ | ||
\ <div class='comments'>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Sally</span>\ | ||
\ <div class='comment text'>Woo hoo!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bill</span>\ | ||
\ <img class='comment image' src='http://example.com/cat.gif' />\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment text'>WTF!?!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment video'>A video? That's new!</div>\ | ||
\ </div>\ | ||
\ </div>\ | ||
\ </body>\ | ||
\</html>" | ||
|
||
type Error = String | ||
|
||
type Author = String | ||
|
||
data Comment | ||
= TextComment Author String | ||
| ImageComment Author URL | ||
deriving (Show, Eq) | ||
|
||
type ScraperWithError a = ScraperT String (Writer [Error]) a | ||
|
||
scrapeStringOrError :: String -> ScraperWithError a -> (Maybe a, [Error]) | ||
scrapeStringOrError html scraper = runWriter $ scrapeStringLikeT html scraper | ||
|
||
main :: IO () | ||
main = print $ scrapeStringOrError exampleHtml comments | ||
where | ||
comments :: ScraperWithError [Comment] | ||
comments = chroots ("div" @: [hasClass "container"]) comment | ||
|
||
logError :: String -> ScraperWithError a | ||
logError message = do | ||
currentHtml <- html anySelector | ||
tell [message ++ currentHtml] | ||
empty | ||
|
||
comment :: ScraperWithError Comment | ||
comment = textComment <|> imageComment <|> logError "Unknown comment type: " | ||
|
||
textComment :: ScraperWithError Comment | ||
textComment = do | ||
author <- text $ "span" @: [hasClass "author"] | ||
commentText <- text $ "div" @: [hasClass "text"] | ||
return $ TextComment author commentText | ||
|
||
imageComment :: ScraperWithError Comment | ||
imageComment = do | ||
author <- text $ "span" @: [hasClass "author"] | ||
imageURL <- attr "src" $ "img" @: [hasClass "image"] | ||
return $ ImageComment author imageURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
import Text.HTML.Scalpel | ||
import Control.Applicative | ||
import Control.Monad.Error.Class (throwError) | ||
import Control.Monad.Writer.Class (tell) | ||
|
||
|
||
exampleHtml :: String | ||
exampleHtml = "<html>\ | ||
\ <body>\ | ||
\ <div class='comments'>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Sally</span>\ | ||
\ <div class='comment text'>Woo hoo!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bill</span>\ | ||
\ <img class='comment image' src='http://example.com/cat.gif' />\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment text'>WTF!?!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment video'>A video? That's new!</div>\ | ||
\ </div>\ | ||
\ </div>\ | ||
\ </body>\ | ||
\</html>" | ||
|
||
type Error = String | ||
|
||
type Author = String | ||
|
||
data Comment | ||
= TextComment Author String | ||
| ImageComment Author URL | ||
deriving (Show, Eq) | ||
|
||
type ScraperWithError a = ScraperT String (Either Error) a | ||
|
||
scrapeStringOrError :: String -> ScraperWithError a -> Either Error a | ||
scrapeStringOrError html scraper | ||
| Left error <- result = Left error | ||
| Right Nothing <- result = Left "Unknown error" | ||
| Right (Just a) <- result = Right a | ||
where | ||
result = scrapeStringLikeT html scraper | ||
|
||
main :: IO () | ||
main = print $ scrapeStringOrError exampleHtml comments | ||
where | ||
comments :: ScraperWithError [Comment] | ||
comments = chroots ("div" @: [hasClass "container"]) comment | ||
|
||
comment :: ScraperWithError Comment | ||
comment = textComment <|> imageComment <|> throwError "Unknown comment type" | ||
|
||
textComment :: ScraperWithError Comment | ||
textComment = do | ||
author <- text $ "span" @: [hasClass "author"] | ||
commentText <- text $ "div" @: [hasClass "text"] | ||
return $ TextComment author commentText | ||
|
||
imageComment :: ScraperWithError Comment | ||
imageComment = do | ||
author <- text $ "span" @: [hasClass "author"] | ||
imageURL <- attr "src" $ "img" @: [hasClass "image"] | ||
return $ ImageComment author imageURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ packages: | |
- scalpel/ | ||
- scalpel-core/ | ||
- examples/ | ||
resolver: lts-13.7 | ||
resolver: lts-18.28 |