Search code examples
haskellscotty

Add print in Scotty do block


I'm very new to Haskell so sorry in advance if the question is silly, but I was not able to find the solution in google.

Let's say that I have this program using the Scotty web framework:

responseUserByName :: ActionM ()
responseUserByName = do name <- param "name"
                        user <- liftAndCatchIO $ getUserByUserName name
                        json user

I would like to add a log since it is failing at runtime and I cannot tell why. So my idea was to add some print in the do block to check values.

But since the do block has to return a ActionM I cannot print and return an IO. Well or at least I don't know how.

Regards


Solution

  • I'm going to guess that ActionM is from Scotty. In that case, you can simply lift IO actions with liftIO, like you already do with liftAndCatchIO:

    responseUserByName :: ActionM ()
    responseUserByName =
        do name <- param "name"
           user <- liftAndCatchIO $ getUserByUserName name
           liftIO $ putStrLn "this is a log message"
           json user