You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
throw envelope errors in a short-circuiting monad #3
Just to get it on the table: You could just use throwIO. You create a custom exception type that is not exported and a type-safe function that throws only that exception within your Envelope. Your Envelope "runner" would always catch this custom exception type and handle it properly.
This may seem hacky, but in some cases I prefer this over pulling out ExceptT which seems like a very heavy tool for the job.
I ended up making an EnvelopeT similar to lexi-lambda's suggestion from #7 (comment). It seems to work alright, although it suffers from the same problems she describes:
dataEnvelopeTesma=EnvelopeT{runEnvelopeT::m (Envelopeesa)
}derivingFunctorinstanceMonadm=>Applicative (EnvelopeTesm) wherepure::a->EnvelopeTesmapure a =EnvelopeT$ pureSuccEnvelope a
(<*>)::EnvelopeTesm (a->b) ->EnvelopeTesma->EnvelopeTesmb(<*>)= ap
instanceMonadm=>Monad (EnvelopeTesm) where(>>=)::EnvelopeTesma-> (a->EnvelopeTesmb) ->EnvelopeTesmb
(EnvelopeT m) >>= k =EnvelopeT$do
env <- m
case env ofSuccEnvelope a -> runEnvelopeT $ k a
ErrEnvelope err ->pure$ErrEnvelope err
instanceMonadTrans (EnvelopeTes) wherelift::Monadm=>ma->EnvelopeTesma
lift m =EnvelopeT$do
val <- m
pureSuccEnvelope val
instanceMonadIOm=>MonadIO (EnvelopeTesm) whereliftIO::IOa->EnvelopeTesma
liftIO = lift . liftIO
pureSuccEnvT::Applicativem=>a->EnvelopeTesma
pureSuccEnvT =EnvelopeT. pureSuccEnvelope
pureErrEnvT:: (Applicativem, IsMemberees) =>e->EnvelopeTesma
pureErrEnvT =EnvelopeT. pureErrEnvelope
Then you can write helper functions for your application like this:
runDbOr404EnvT::
( HasPoolr
, IsMemberDbNotFoundErres
, MonadBaseControlIOm
, MonadReaderrm
)
=>ReaderTSqlBackendm (Maybea)
->EnvelopeTesma
runDbOr404EnvT query =do
pool' <- lift $ view pool
maybeRes <- lift $ runSqlPool query pool'
case maybeRes ofJust a -> pureSuccEnvT a
Nothing-> pureErrEnvT DbNotFoundErr
I haven't yet checked the laws for EnvelopeT, but it is effectively ExceptT, so I'm thinking it is probably fine.
Activity
cdepillabout commentedon May 7, 2017
@lexi-lambda Left some notes on playing around with this at #7 (comment)
3noch commentedon May 19, 2017
Just to get it on the table: You could just use
throwIO. You create a custom exception type that is not exported and a type-safe function that throws only that exception within yourEnvelope. YourEnvelope"runner" would always catch this custom exception type and handle it properly.This may seem hacky, but in some cases I prefer this over pulling out
ExceptTwhich seems like a very heavy tool for the job.cdepillabout commentedon Mar 8, 2018
I ended up making an
EnvelopeTsimilar to lexi-lambda's suggestion from #7 (comment). It seems to work alright, although it suffers from the same problems she describes:Then you can write helper functions for your application like this:
I haven't yet checked the laws for
EnvelopeT, but it is effectivelyExceptT, so I'm thinking it is probably fine.23Skidoo commentedon Jul 4, 2019
Would be nice if there was a blessed implementation of this.
cdepillabout commentedon Jul 6, 2019
@23Skidoo I'm working on putting this together for you.
cdepillabout commentedon Jul 8, 2019
@23Skidoo I just sent a PR adding an
EnvelopeTtransformer similar to what is in a comment above.I also made a release to hackage with
EnvelopeT:http://hackage.haskell.org/package/servant-checked-exceptions-2.2.0.0
This doesn't add an mtl-like
MonadEnvelopetype class, but if someone wants to put together a PR adding something like that, I'd accept it.23Skidoo commentedon Jul 8, 2019
Awesome, thanks!