[Haskell-cafe] ANNOUNCE: new installment of failure framework
pepeiborra at gmail.com
Tue Dec 8 08:08:46 EST 2009
On Dec 8, 2009, at 1:47 PM, Michael Snoyman wrote:
> For example, let's say I want to write some code to authenticate a user via OpenID (see the authenticate package). It has to do at least two things:
> * Download pages by HTTP
> * Parse the resulting HTML page.
> I would like to ideally do the following:
> authenticate = do
> page <- getPage url
> parsed <- parseHtml page
> checkResult parsed
> In other words, easily chain things together, and simply let the error types propogate. If the given functions had these signatures:
> getPage :: (MonadFailure HttpException m, MonadIO m) => String -> m String
> parseHtml :: Failure ParseHtmlException m => String -> m HtmlTree
> checkResult :: Failure AuthenticationException m => HtmlTree -> m Identifier
> Then authenticate would simply subsume all these error types, making it explicit what a client of the libraries needs to be aware of.
Let me highlight this point. The Maybe, Either,Exception diversity can be a curse instead of a blessing when combining libraries which return errors in different ways.
The main goal of the failure framework is to provide an abstract notion of failure, so that libraries can offer interfaces which fail in an abstract way, and the client of the libraries can make the choice of how to manage the failures.
Using a similar example (from ), consider a program to download a web page and parse it:
1. Network.URI.parseURI returns (Maybe URI).
2. Network.HTTP.simpleHTTP returns (IO (Result Request)), which is basically a broken version of (IO (Either ConnError Request)).
3. Parsec returns (Either ParseError a)
So there's no hope that I can write anything like:
*> do uri <- parseURI uriStr
*> doc <- evenSimplerHTTP uri
*> parsed <- parse grammar uriStr doc
The Failure package contains functions that help when dealing with this problem, so that we can write something very close:
> do uri <- try <$> parseURI uriStr
> doc <- try <$> evenSimplerHTTP uri
> let parsed = try $ parse grammar uriStr doc
But more importantly, the abstract notion of Failure enables library authors to write code that will easily combine with other failable code.
 - http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors
More information about the Haskell-Cafe