Skip to content

Commit

Permalink
handle ActionError prior to a user-defined handler (fixes #330) (#331)
Browse files Browse the repository at this point in the history
Catching SomeException in a user-defined handler should not bypass actionErrorHandler.
  • Loading branch information
fumieval authored Oct 13, 2023
1 parent a733383 commit 123ff5f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Web/Scotty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import Network.Wai (Application, Middleware, Request, StreamingBody)
import Network.Wai.Handler.Warp (Port)

import Web.Scotty.Internal.Types (ScottyT, ActionT, ErrorHandler, Param, RoutePattern, Options, defaultOptions, File, Kilobytes, ScottyState, defaultScottyState, StatusError(..), Content(..))
import Web.Scotty.Exceptions (Handler(..))
import UnliftIO.Exception (Handler(..))

type ScottyM = ScottyT IO
type ActionM = ActionT IO
Expand Down
15 changes: 7 additions & 8 deletions Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.CaseInsensitive as CI
import Data.Int
import Data.Maybe (maybeToList)
import qualified Data.Text as ST
import qualified Data.Text.Encoding as STE
import qualified Data.Text.Lazy as T
Expand All @@ -86,7 +87,7 @@ import Numeric.Natural

import Web.Scotty.Internal.Types
import Web.Scotty.Util (mkResponse, addIfNotPresent, add, replace, lazyTextToStrictByteString, strictByteStringToLazyText)
import Web.Scotty.Exceptions (Handler(..), catch, catchesOptionally, tryAny)
import UnliftIO.Exception (Handler(..), catch, catches, tryAny)

import Network.Wai.Internal (ResponseReceived(..))

Expand All @@ -101,13 +102,11 @@ runAction :: MonadUnliftIO m =>
-> ActionT m () -- ^ Route action to be evaluated
-> m (Maybe Response)
runAction mh env action = do
let
handlers = [
statusErrorHandler, -- StatusError
actionErrorHandler, -- ActionError i.e. Next, Finish, Redirect
someExceptionHandler -- all remaining exceptions
]
ok <- flip runReaderT env $ runAM $ tryNext (catchesOptionally action mh handlers )
ok <- flip runReaderT env $ runAM $ tryNext $ action `catches` concat
[ [actionErrorHandler]
, maybeToList mh
, [statusErrorHandler, someExceptionHandler]
]
res <- getResponse env
return $ bool Nothing (Just $ mkResponse res) ok

Expand Down
25 changes: 0 additions & 25 deletions Web/Scotty/Exceptions.hs

This file was deleted.

2 changes: 1 addition & 1 deletion Web/Scotty/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import qualified Network.Wai as Wai
import Network.Wai.Handler.Warp (Settings, defaultSettings)
import Network.Wai.Parse (FileInfo)

import Web.Scotty.Exceptions (Handler(..), catch, catches)
import UnliftIO.Exception (Handler(..), catch, catches)


--------------------- Options -----------------------
Expand Down
2 changes: 1 addition & 1 deletion Web/Scotty/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import Web.Scotty.Route
import Web.Scotty.Internal.Types (ActionT(..), ScottyT(..), defaultScottyState, Application, RoutePattern, Options(..), defaultOptions, RouteOptions(..), defaultRouteOptions, ErrorHandler, Kilobytes, File, addMiddleware, setHandler, updateMaxRequestBodySize, routes, middlewares, ScottyException(..), ScottyState, defaultScottyState, StatusError(..), Content(..))
import Web.Scotty.Util (socketDescription)
import Web.Scotty.Body (newBodyInfo)
import Web.Scotty.Exceptions (Handler(..), catches)
import UnliftIO.Exception (Handler(..), catches)

-- | Run a scotty application using the warp server.
-- NB: scotty p === scottyT p id
Expand Down
1 change: 0 additions & 1 deletion scotty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Library
Web.Scotty.Cookie
other-modules: Web.Scotty.Action
Web.Scotty.Body
Web.Scotty.Exceptions
Web.Scotty.Route
Web.Scotty.Util
default-language: Haskell2010
Expand Down

0 comments on commit 123ff5f

Please sign in to comment.