diff --git a/src/Middleware.hs b/src/Middleware.hs index 47658ef8d..9045c4703 100644 --- a/src/Middleware.hs +++ b/src/Middleware.hs @@ -18,7 +18,7 @@ import Control.Exception (finally, throw, catchJust, catch, SomeException, import Network.HTTP.Types.Header (RequestHeaders, hContentType, hAuthorization, hLocation) -import Network.HTTP.Types.Status (status400, status401, status301) +import Network.HTTP.Types.Status (status400, status401, status404, status301) import Network.Wai (Application, requestHeaders, responseLBS, rawPathInfo, rawQueryString, isSecure) import Network.URI (URI(..), parseURI) @@ -72,10 +72,10 @@ instance ToJSON SqlError where clientErrors :: Application -> Application clientErrors app req respond = - catchJust isPgException (app req respond) ( - respond . responseLBS status400 [(hContentType, "application/json")] - . encode - ) + catchJust isPgException (app req respond) $ \err -> + respond $ if seState err == "42P01" + then responseLBS status404 [] "" + else responseLBS status400 [(hContentType, "application/json")] (encode err) where isPgException :: SqlError -> Maybe SqlError diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 37fad40fc..fcfad55ce 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -9,6 +9,10 @@ import SpecHelper spec :: Spec spec = around appWithFixture $ do + describe "Querying a nonexistent table" $ + it "causes a 404" $ + get "/faketable" `shouldRespondWith` 404 + describe "Filtering response" $ context "column equality" $