-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
more general/abstract error handling #45
Comments
Where did you find this semantics of Alternative? The only meaning of <|> - it's a binary associative operation on applicative functors. Look at the implementation of alternative for ReaderT. It only lifts whatever underlying monad implements. There is no Alternative for IO. |
Uhm, that's basically how people use this, including the most famous Parser libraries. And it's also the reason this class is called
Yes, the problem is the error handling which is done via fetch :: (MonadIO m) => Query -> Action m Document
fetch q = findOne q >>= maybe (liftIO $ throwIO $ DocNotFound $ selection q) return I am sure this could be done more general than invoking |
If you want a better error handling, I agree, there is a lot of room for improvement in this area, but it's not about Alternative. In most of the cases your <|> won't work because in most of the cases m is IO and IO is not an instance of Alternative. So, you should avoid using <|> in any case. v <- access p master "admin" (serverVersion <|> serverVersion) |
But for I did some testing and got weird results, e.g. the following fetch :: (MonadIO m, MonadThrow m) => Query -> Action m Document
fetch q = findOne q >>= maybe (throwM $ userError "") return However, trying that with our own Exception type does not work, and there's no indication why, since the types seem to match: fetch :: (MonadIO m, MonadThrow m) => Query -> Action m Document
fetch q = findOne q >>= maybe (throwM $ DocNotFound $ selection q) return Or simpler, in ghci:
It's not clear to me where the difference comes from. It seems |
I suggest to rename this issue to something link: introduce a more general error reporting to the driver. |
The reason is https://hackage.haskell.org/package/transformers-0.5.2.0/docs/src/Control.Monad.Trans.Error.html instance MonadPlus IO where
mzero = ioError (userError "mzero")
m `mplus` n = m `catchIOError` \ _ -> n So Alternative uses |
After a lengthy discussion in #haskell people were suggesting to use a custom infix operator instead of Edit: even that seems complicated, because the current error handling is a wild mixture of
|
I ran into another manifestation of this.
|
I expected something like this to work:
Instead, I just get an Exception. Using
findOne
doesn't work either, since it's always a success.IMO, the exception should only happen if the last alternative fails. At least that's the expected semantics of
(<|>)
.The text was updated successfully, but these errors were encountered: