[Haskell-cafe] propogation of Error

Daniel Gorín dgorin at dc.uba.ar
Fri Dec 5 07:52:39 EST 2008

> i would expect to get back the Error from the *first* function in the
> sequence of functions in checkHeader (oggHeaderError from the  
> oggHeader
> function). but instead i always see the Error from the *last* function
> in the sequence, OggPacketFlagError from the OggPacketFlag function.  
> why
> is this? is there any way i can get the desired behavior...i.e. see  
> the
> Error from the first function in the sequence that fails?


You are essentially asking why this function:

checkHeader handle = ((oggHeader handle)       >>
                       (oggStreamFlag handle)   >>
                       (oggHeaderFlag handle)   >>
                       (skipBytes handle 20)    >>
                       (oggPageSecCount handle) >>
                       (oggPacketFlag handle))

returns the last error (OggPacketFlagError) instead of the first one.  
Some type annotations might help you see what is going on. So let's  
ask ghci the type of, e.g. oggHeaderFlag

*File.Ogg> :t oggHeaderFlag
oggHeaderFlag :: SIO.Handle -> IO (Either OggParseErrorType [Char])

oggHeaderFlag takes a handle, and computes either an error or a  
string. But since you are using >>, the computed value is not passed  
to the next function in the pipe! There is no way checkHeader can stop  
early simply because it is ignoring the intermediate results altogether.

Since you are importing Control.Monad.Error, I believe you would  
probably want oggHeaderFlag et al to have type:

SIO.Handle -> ErrorT OggParseErrorType IO [Char]

This will propagate errors correctly.

You can see a version of your code using ErrorT here: http://hpaste.org/12705#a1


More information about the Haskell-Cafe mailing list