Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created October 6, 2011 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoyberg/1267589 to your computer and use it in GitHub Desktop.
Save snoyberg/1267589 to your computer and use it in GitHub Desktop.
Catch exceptions in an Iteratee
import Control.Exception (Exception, catch)
import Data.Enumerator (Iteratee (..), Step (..))
import Data.ByteString (ByteString)
import Prelude hiding (catch)
catchIter :: Exception e
=> Iteratee ByteString IO a
-> (e -> Iteratee ByteString IO a)
-> Iteratee ByteString IO a
catchIter (Iteratee mstep) f = Iteratee $ do
step <- mstep `catch` (runIteratee . f)
return $ case step of
Continue k -> Continue $ \s -> catchIter (k s) f
Yield b s -> Yield b s
Error e -> Error e
@akaspin
Copy link

akaspin commented Nov 6, 2011

Not works for errors such divide by zero e.t.c. In this case flow not reach line 15.

UPD: Sorry. Only for lazy functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment