Handle missing table sql errors with 404

This commit is contained in:
Joe Nelson
2014-12-06 17:42:20 -08:00
parent 21b0b02c03
commit eb7e9586ab
3 changed files with 25 additions and 8 deletions
+14 -1
View File
@@ -1,10 +1,11 @@
{-# LANGUAGE FlexibleContexts #-}
module App (app) where
module App (app, sqlErrHandler, isSqlError) where
import Control.Monad (join)
import Control.Arrow ((***))
import Control.Applicative
import Control.Monad.IO.Class (liftIO, MonadIO)
-- import Control.Exception.Base
import Data.Text hiding (map)
import Data.Maybe (fromMaybe)
@@ -26,6 +27,7 @@ import Data.Aeson
import Data.Coerce
import Data.Monoid
import qualified Hasql as H
import qualified Hasql.Backend as HB
import qualified Hasql.Postgres as H
import PgQuery
@@ -158,6 +160,17 @@ app req =
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
isSqlError :: HB.Error -> Maybe HB.Error
isSqlError (HB.ErroneousResult x) = Just $ HB.ErroneousResult x
isSqlError _ = Nothing
sqlErrHandler :: HB.Error -> IO Response
sqlErrHandler (HB.ErroneousResult err) = do
return $ if "42P01" `isInfixOf` err
then responseLBS status404 [] ""
else responseLBS status400 [] (cs err)
sqlErrHandler _ = error "just for debugging"
rangeStatus :: Int -> Int -> Int -> Status
rangeStatus from to total
| from > total = status416