Skip to content
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

Print errors from action handlers in stdout instead of ignoring them #197

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ processAction BotApp{..} botEnv@BotEnv{..} update action = do
(newModel, effects) -> do
writeTVar botModelVar newModel
return effects
mapM_ ((liftIO . issueAction botEnv update) <=< runBotM (BotContext botUser update)) effects
let runBotAndIssueAction
= (liftIO . issueAction botEnv update) <=< runBotM (BotContext botUser update)
mapM_ runBotAndIssueAction effects

-- | A job to wait for the next action and process it.
processActionJob :: BotApp model action -> BotEnv model action -> ClientM ()
Expand All @@ -122,7 +124,10 @@ processActionsIndefinitely
:: BotApp model action -> BotEnv model action -> IO ThreadId
processActionsIndefinitely botApp botEnv = do
a <- asyncLink $ forever $ do
runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)
res <- runClientM (processActionJob botApp botEnv) (botClientEnv botEnv)
case res of
Left err -> print err
Right _ -> return ()
return (asyncThreadId a)

-- | Start 'Telegram.Update' polling for a bot.
Expand Down
3 changes: 3 additions & 0 deletions telegram-bot-simple/src/Telegram/Bot/Simple/Eff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ data BotContext = BotContext
, botContextUpdate :: Maybe Telegram.Update
}

-- | To retain control over errors coming from Telegram API in `BotM`,
-- consider using `liftIO . flip runClientM clientEnv` instead.
-- Issues generated by servant-client will be logged, response failure errors will be ignored.
liftClientM :: ClientM a -> BotM a
liftClientM = BotM . lift

Expand Down
Loading