-
Notifications
You must be signed in to change notification settings - Fork 133
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 unhandled exception to stdout depending on verbosity #374
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
@@ -82,6 +83,7 @@ | |
import qualified Data.Text.Lazy.Encoding as TLE | ||
import Data.Time (UTCTime) | ||
import Data.Time.Format (parseTimeM, defaultTimeLocale) | ||
import Data.Typeable (typeOf) | ||
import Data.Word | ||
|
||
import Network.HTTP.Types | ||
|
@@ -94,7 +96,7 @@ | |
import Numeric.Natural | ||
|
||
import Web.Scotty.Internal.Types | ||
import Web.Scotty.Util (mkResponse, addIfNotPresent, add, replace, lazyTextToStrictByteString, decodeUtf8Lenient) | ||
Check warning on line 99 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.6.3
Check warning on line 99 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.4.6
|
||
import UnliftIO.Exception (Handler(..), catch, catches, throwIO) | ||
|
||
import Network.Wai.Internal (ResponseReceived(..)) | ||
|
@@ -105,15 +107,16 @@ | |
-- 'Nothing' indicates route failed (due to Next) and pattern matching should try the next available route. | ||
-- 'Just' indicates a successful response. | ||
runAction :: MonadUnliftIO m => | ||
Maybe (ErrorHandler m) -- ^ this handler (if present) is in charge of user-defined exceptions | ||
Options | ||
-> Maybe (ErrorHandler m) -- ^ this handler (if present) is in charge of user-defined exceptions | ||
-> ActionEnv | ||
-> ActionT m () -- ^ Route action to be evaluated | ||
-> m (Maybe Response) | ||
runAction mh env action = do | ||
runAction options mh env action = do | ||
ok <- flip runReaderT env $ runAM $ tryNext $ action `catches` concat | ||
[ [actionErrorHandler] | ||
, maybeToList mh | ||
, [statusErrorHandler, scottyExceptionHandler, someExceptionHandler] | ||
, [statusErrorHandler, scottyExceptionHandler, someExceptionHandler options] | ||
] | ||
res <- getResponse env | ||
return $ bool Nothing (Just $ mkResponse res) ok | ||
|
@@ -121,7 +124,7 @@ | |
-- | Catches 'StatusError' and produces an appropriate HTTP response. | ||
statusErrorHandler :: MonadIO m => ErrorHandler m | ||
statusErrorHandler = Handler $ \case | ||
StatusError s e -> do | ||
Check warning on line 127 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.6.3
Check warning on line 127 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.2.8
Check warning on line 127 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.4.6
Check warning on line 127 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.6.2
Check warning on line 127 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 8.10.7
|
||
status s | ||
let code = T.pack $ show $ statusCode s | ||
let msg = decodeUtf8Lenient $ statusMessage s | ||
|
@@ -171,9 +174,13 @@ | |
text $ T.unwords [ "Failed to parse parameter", k, v, ":", e] | ||
|
||
-- | Uncaught exceptions turn into HTTP 500 Server Error codes | ||
someExceptionHandler :: MonadIO m => ErrorHandler m | ||
someExceptionHandler = Handler $ \case | ||
(_ :: E.SomeException) -> status status500 | ||
someExceptionHandler :: MonadIO m => Options -> ErrorHandler m | ||
someExceptionHandler Options{verbose} = | ||
Handler $ \(E.SomeException e) -> do | ||
when (verbose > 0) $ | ||
liftIO $ | ||
putStrLn $ "Caught and exception of " <> show (typeOf e) <> ": " <> show e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that it goes to stdout, not stderr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://12factor.net/logs recommend stdout, but I'm not sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong opinion; I think stdout is fine too. By the way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed typo |
||
status status500 | ||
|
||
-- | Throw a "500 Server Error" 'StatusError', which can be caught with 'catch'. | ||
-- | ||
|
@@ -188,7 +195,7 @@ | |
-- | ||
-- Uncaught exceptions turn into HTTP responses corresponding to the given status. | ||
raiseStatus :: Monad m => Status -> T.Text -> ActionT m a | ||
raiseStatus s = E.throw . StatusError s | ||
Check warning on line 198 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.6.3
Check warning on line 198 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.2.8
Check warning on line 198 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.4.6
Check warning on line 198 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 9.6.2
Check warning on line 198 in Web/Scotty/Action.hs GitHub Actions / ubuntu-latest / ghc 8.10.7
|
||
{-# DEPRECATED raiseStatus "Use status, text, and finish instead" #-} | ||
|
||
-- | Throw an exception which can be caught within the scope of the current Action with 'catch'. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows to read record fields in the most natural way (in my taste):