Something that happens to me during web programming: I want to start an operation that has a chance of failure. If I refuse, I want to send 500 to the client. Usually, I just want to continue with a series of steps.
doSomeWebStuff :: SomeWebMonad () doSomeWebStuff = do res <- databaseCall case res of Left err -> status 500 Right val -> do res2 <- anotherDatabaseCall (someprop val) case res2 of Left err -> status 500 Right val2 -> text $ show val2
since errors are exceptions, I don’t like that I need all these things to catch them. I want to do the same when something is left. Is there a way to express this on the same line as something like guard , but control what it returns in the output?
In another language, I could do this:
function doSomeWebStuff() { var res = databaseCall() if (res == Error) return status 500 var res2 = anotherDatabaseCall(res.someprop) if (res2 == Error) return status 500 return text(res2) }
So, I am good at writing some kind of template, but I don’t want errors messing around with my socket when it is much more common, just to continue working with the case found.
What is the cleanest way to do this? I know that theoretically I can use the monad to go out at an early stage of failure, but I only saw examples with Maybe , and it would return Nothing at the end, instead of letting me indicate what it returns.
source share