From 54453175d4077cd369b37ad8af045f2f9063b892 Mon Sep 17 00:00:00 2001 From: Yuriy Syrovetskiy Date: Sat, 10 Feb 2024 16:33:13 +0100 Subject: [PATCH 1/2] Add test on handler priority --- test/Web/ScottySpec.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index fcbe860..142fdfe 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -65,7 +65,7 @@ spec = do makeRequest "/:paramName" `shouldRespondWith` ":paramName" it ("captures route parameters for " ++ method ++ " requests with url encoded '/' in path") $ do makeRequest "/a%2Fb" `shouldRespondWith` "a/b" - + describe "addroute" $ do forM_ availableMethods $ \method -> do withApp (addroute method "/scotty" $ html "") $ do @@ -108,9 +108,11 @@ spec = do withApp (do let h = Handler (\(_ :: E.SomeException) -> setHeader "Location" "/c" >> status status500) defaultHandler h - Scotty.get "/a" (redirect "/b")) $ do + Scotty.get "/a" (redirect "/b") + Scotty.get "/d" (raiseStatus status404 "d not found")) $ do it "should give priority to actionErrorHandlers" $ do get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] } + get "/d" `shouldRespondWith` "d not found" { matchStatus = 404 } context "when not specified" $ do withApp (Scotty.get "/" $ throw E.DivideByZero) $ do From 6c5c2c23b4eafa6724721bc63a0f94c600998538 Mon Sep 17 00:00:00 2001 From: Yuriy Syrovetskiy Date: Sun, 11 Feb 2024 23:47:01 +0100 Subject: [PATCH 2/2] fixup! Add test on handler priority --- Web/Scotty/Action.hs | 4 ++-- test/Web/ScottySpec.hs | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Web/Scotty/Action.hs b/Web/Scotty/Action.hs index f625d6c..0f6afcc 100644 --- a/Web/Scotty/Action.hs +++ b/Web/Scotty/Action.hs @@ -111,9 +111,9 @@ runAction :: MonadUnliftIO m => -> m (Maybe Response) runAction mh env action = do ok <- flip runReaderT env $ runAM $ tryNext $ action `catches` concat - [ [actionErrorHandler] + [ [actionErrorHandler, statusErrorHandler, scottyExceptionHandler] , maybeToList mh - , [statusErrorHandler, scottyExceptionHandler, someExceptionHandler] + , [someExceptionHandler] ] res <- getResponse env return $ bool Nothing (Just $ mkResponse res) ok diff --git a/test/Web/ScottySpec.hs b/test/Web/ScottySpec.hs index 142fdfe..0476cc6 100644 --- a/test/Web/ScottySpec.hs +++ b/test/Web/ScottySpec.hs @@ -112,7 +112,9 @@ spec = do Scotty.get "/d" (raiseStatus status404 "d not found")) $ do it "should give priority to actionErrorHandlers" $ do get "/a" `shouldRespondWith` 302 { matchHeaders = ["Location" <:> "/b"] } - get "/d" `shouldRespondWith` "d not found" { matchStatus = 404 } + get "/d" + `shouldRespondWith` + "

404 Not Found

d not found" { matchStatus = 404 } context "when not specified" $ do withApp (Scotty.get "/" $ throw E.DivideByZero) $ do