
donn:
Quoth Dimitry Golubovsky
, Is there any way to catch/detect failures inside the Get monad? It is not an instance of MonadError, so catchError does not work.
Ideally, the function would keep decoding as long as it is possible, and upon the first failure of the parser, return whatever has been decoded.
I believe it can't be done. (I've seen three responses that seemed to be proposing some course of action, but ... correct me if I'm wrong, nothing that would allow you to use Get in Data.Binary this way.)
The key point is the use of `throw', via `error', in Get's `fail'. `throw' raises an exception that can be caught only in IO, so you can't catch it inside Get. So ... while `fail' is a Monad function, it isn't implemented here in a way you could use, like it is in Maybe for example. Nor could it be, I think, which is kind of unfortunate.
For strict, checked binary parsing, use the cereal package. For lazy binary parsing with async errors, use binary. They're the main two points in the design space. The other is to tag the lazy stream, and insert failure tags in the structure. -- Don