Skip to content

Commit

Permalink
Adding exception handling for those making it to the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Dec 18, 2024
1 parent b53ff58 commit 455973a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
module Server (startApp, makeConfig) where

import Control.Concurrent (newChan, writeChan)
import Control.Exception (SomeException (SomeException), throwIO, try)
import Control.Exception (Exception (fromException), SomeAsyncException (SomeAsyncException), SomeException (SomeException), throwIO, try)
import Control.Monad.Reader
import Data.Foldable (for_)
import Data.IORef (newIORef)
import Data.Maybe (fromJust, fromMaybe)
import qualified Data.Text as T
import GHC.IO (catch, onException)
import Jobs
import Mongo (setupDb)
import Network.Wai
Expand Down Expand Up @@ -151,6 +152,14 @@ startApp = do
initStart config
putStrLn $ "Startup completed for version " ++ T.unpack (app_version config)
putStrLn $ "Running now using " ++ show port
run port $ withServer config
run port (withServer config)
`catch` ( \e -> do
let (analysis, err) = case fromException e of
Just (SomeAsyncException _) -> ("async", show e)
_ -> ("synchronous", show e)
report = "An exception (" ++ analysis ++ ") made it to the main thread, rethrowing after logging: " ++ show err
print report
throwIO e
)
where
port = 8000

0 comments on commit 455973a

Please sign in to comment.