
brad clawsie wrote:
i am working on improving a module for getting Yahoo Finance quote data, hopefully getting it to a point that i can put it on hackage
in the quote retrieval function, there are a few places i would like to call out errors. in a trivial case i could return
IO (Maybe String)
with Nothing signifying any error state, or Just expressing the data
but i would like to be able to express some of these error cases in a more structured manner
i know the Either type can be used in such a case(?), but i've had some problem locating a satisfactory example (if this is indeed appropriate)
could one of the vets here provide a simplistic example expressing error cases, preferrably in the IO Monad (in case there are any gotchas there)?
It's fairly common to use the Either type for this. By convention, "Right" means "correct", and by elimination "Left" means an error... foo x = case x of .... return (Right y) .... return (Left "Some error happened") Instead of just returning a text string indicating the error condition, you could devize your own special type for representing the possible errors that can happen. You could also make a kind of a "Result" type that represents both successful *and* failed results, if you prefer. In addition, in the IO monad, I believe you can do fun stuff with exceptions. Specifically, you can *catch* them. However, beware: Haskell has something of a habit of executing stuff at unpredictable times, which means that if you're throwing errors in pure code, it can be hard to register the exception handler(s) at the right time! Probably simpler and clearer to go with Either or something OTOH, if you're throwing errors from within the IO monad itself, timing should be less of an issue. Just my few cents...