Skip to content

Commit

Permalink
Merge pull request #197 from fizruk/log-api-exceptions-from-action-ha…
Browse files Browse the repository at this point in the history
…ndlers
  • Loading branch information
swamp-agr authored Nov 7, 2024
2 parents 027648c + 4018bfd commit 38be21d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 38be21d

Please sign in to comment.