Removes error case from Payload type. Now we don't build a payload when the parsing fails.
This commit is contained in:
+17
-28
@@ -15,8 +15,6 @@ module PostgREST.ApiRequest ( ApiRequest(..)
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Protolude
|
import Protolude
|
||||||
import Data.Ranged.Ranges (emptyRange)
|
|
||||||
|
|
||||||
import qualified Data.Aeson as JSON
|
import qualified Data.Aeson as JSON
|
||||||
import qualified Data.ByteString as BS
|
import qualified Data.ByteString as BS
|
||||||
import qualified Data.ByteString.Internal as BS (c2w)
|
import qualified Data.ByteString.Internal as BS (c2w)
|
||||||
@@ -40,7 +38,7 @@ import Data.Ranged.Boundaries
|
|||||||
import PostgREST.Types (QualifiedIdentifier (..),
|
import PostgREST.Types (QualifiedIdentifier (..),
|
||||||
Schema, Payload(..),
|
Schema, Payload(..),
|
||||||
UniformObjects(..))
|
UniformObjects(..))
|
||||||
import Data.Ranged.Ranges (Range(..), singletonRange, rangeIntersection)
|
import Data.Ranged.Ranges (Range(..), singletonRange, rangeIntersection, emptyRange)
|
||||||
|
|
||||||
type RequestBody = BL.ByteString
|
type RequestBody = BL.ByteString
|
||||||
|
|
||||||
@@ -122,8 +120,8 @@ data ApiRequest = ApiRequest {
|
|||||||
userApiRequest :: Schema -> Request -> RequestBody -> Either ApiRequestError ApiRequest
|
userApiRequest :: Schema -> Request -> RequestBody -> Either ApiRequestError ApiRequest
|
||||||
userApiRequest schema req reqBody
|
userApiRequest schema req reqBody
|
||||||
| isTargetingProc && method /= "POST" = Left ErrorActionInappropriate
|
| isTargetingProc && method /= "POST" = Left ErrorActionInappropriate
|
||||||
| topLevelRange == emptyRange = Left $ ErrorInvalidRange
|
| topLevelRange == emptyRange = Left ErrorInvalidRange
|
||||||
| isError = Left $ ErrorInvalidBody payloadError
|
| shouldParsePayload && isLeft payload = either (Left . ErrorInvalidBody . toS) undefined payload
|
||||||
| otherwise = Right ApiRequest {
|
| otherwise = Right ApiRequest {
|
||||||
iAction = action
|
iAction = action
|
||||||
, iTarget = target
|
, iTarget = target
|
||||||
@@ -147,32 +145,22 @@ userApiRequest schema req reqBody
|
|||||||
}
|
}
|
||||||
where
|
where
|
||||||
isTargetingProc = fromMaybe False $ (== "rpc") <$> listToMaybe path
|
isTargetingProc = fromMaybe False $ (== "rpc") <$> listToMaybe path
|
||||||
payloadError = case payload of
|
|
||||||
PayloadParseError err -> err
|
|
||||||
_ -> ""
|
|
||||||
isError = case relevantPayload of
|
|
||||||
Just (PayloadParseError _) -> True
|
|
||||||
_ -> False
|
|
||||||
payload =
|
payload =
|
||||||
case decodeContentType . fromMaybe "application/json" $ lookupHeader "content-type" of
|
case decodeContentType . fromMaybe "application/json" $ lookupHeader "content-type" of
|
||||||
CTApplicationJSON ->
|
CTApplicationJSON ->
|
||||||
either (PayloadParseError . toS)
|
either Left (\val -> case ensureUniform (pluralize val) of
|
||||||
(\val -> case ensureUniform (pluralize val) of
|
Nothing -> Left "All object keys must match"
|
||||||
Nothing -> PayloadParseError "All object keys must match"
|
Just json -> Right $ PayloadJSON json) (JSON.eitherDecode reqBody)
|
||||||
Just json -> PayloadJSON json)
|
|
||||||
(JSON.eitherDecode reqBody)
|
|
||||||
CTTextCSV ->
|
CTTextCSV ->
|
||||||
either (PayloadParseError . toS)
|
either Left (\val -> case ensureUniform (csvToJson val) of
|
||||||
(\val -> case ensureUniform (csvToJson val) of
|
Nothing -> Left "All lines must have same number of fields"
|
||||||
Nothing -> PayloadParseError "All lines must have same number of fields"
|
Just json -> Right $ PayloadJSON json) (CSV.decodeByName reqBody)
|
||||||
Just json -> PayloadJSON json)
|
|
||||||
(CSV.decodeByName reqBody)
|
|
||||||
CTOther "application/x-www-form-urlencoded" ->
|
CTOther "application/x-www-form-urlencoded" ->
|
||||||
PayloadJSON . UniformObjects . V.singleton . M.fromList
|
Right . PayloadJSON . UniformObjects . V.singleton . M.fromList
|
||||||
. map (toS *** JSON.String . toS) . parseSimpleQuery
|
. map (toS *** JSON.String . toS) . parseSimpleQuery
|
||||||
$ toS reqBody
|
$ toS reqBody
|
||||||
ct ->
|
ct ->
|
||||||
PayloadParseError $ "Content-Type not acceptable: " <> toMime ct
|
Left $ toS $ "Content-Type not acceptable: " <> toMime ct
|
||||||
topLevelRange = fromMaybe allRange $ M.lookup "limit" ranges
|
topLevelRange = fromMaybe allRange $ M.lookup "limit" ranges
|
||||||
action =
|
action =
|
||||||
if isTargetingProc
|
if isTargetingProc
|
||||||
@@ -194,11 +182,12 @@ userApiRequest schema req reqBody
|
|||||||
["rpc", proc] -> TargetProc
|
["rpc", proc] -> TargetProc
|
||||||
$ QualifiedIdentifier schema proc
|
$ QualifiedIdentifier schema proc
|
||||||
other -> TargetUnknown other
|
other -> TargetUnknown other
|
||||||
relevantPayload = case action of
|
shouldParsePayload = action `elem` [ActionCreate, ActionUpdate, ActionInvoke]
|
||||||
ActionCreate -> Just payload
|
relevantPayload = if shouldParsePayload
|
||||||
ActionUpdate -> Just payload
|
then case payload of
|
||||||
ActionInvoke -> Just payload
|
Right p -> Just p
|
||||||
_ -> Nothing
|
Left _ -> Nothing
|
||||||
|
else Nothing
|
||||||
path = pathInfo req
|
path = pathInfo req
|
||||||
method = requestMethod req
|
method = requestMethod req
|
||||||
hdrs = requestHeaders req
|
hdrs = requestHeaders req
|
||||||
|
|||||||
@@ -113,7 +113,6 @@ createReadStatement selectQuery countQuery isSingle countTotal asCsv =
|
|||||||
createWriteStatement :: QualifiedIdentifier -> SqlQuery -> SqlQuery -> Bool ->
|
createWriteStatement :: QualifiedIdentifier -> SqlQuery -> SqlQuery -> Bool ->
|
||||||
PreferRepresentation -> [Text] -> Bool -> Payload ->
|
PreferRepresentation -> [Text] -> Bool -> Payload ->
|
||||||
H.Query UniformObjects (Maybe ResultsWithCount)
|
H.Query UniformObjects (Maybe ResultsWithCount)
|
||||||
createWriteStatement _ _ _ _ _ _ _ (PayloadParseError _) = undefined
|
|
||||||
createWriteStatement _ _ mutateQuery _ None
|
createWriteStatement _ _ mutateQuery _ None
|
||||||
_ _ (PayloadJSON (UniformObjects _)) =
|
_ _ (PayloadJSON (UniformObjects _)) =
|
||||||
unicodeStatement sql encodeUniformObjs decodeStandardMay True
|
unicodeStatement sql encodeUniformObjs decodeStandardMay True
|
||||||
@@ -322,8 +321,6 @@ requestToCountQuery schema (DbRead (Node (Select _ _ conditions _ _, (mainTbl, _
|
|||||||
localConditions = filter fn conditions
|
localConditions = filter fn conditions
|
||||||
|
|
||||||
requestToQuery :: Schema -> Bool -> DbRequest -> SqlQuery
|
requestToQuery :: Schema -> Bool -> DbRequest -> SqlQuery
|
||||||
requestToQuery _ _ (DbMutate (Insert _ (PayloadParseError _))) = undefined
|
|
||||||
requestToQuery _ _ (DbMutate (Update _ (PayloadParseError _) _)) = undefined
|
|
||||||
requestToQuery schema isParent (DbRead (Node (Select colSelects tbls conditions ord range, (nodeName, maybeRelation, _)) forest)) =
|
requestToQuery schema isParent (DbRead (Node (Select colSelects tbls conditions ord range, (nodeName, maybeRelation, _)) forest)) =
|
||||||
query
|
query
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -110,9 +110,7 @@ unUniformObjects (UniformObjects objs) = objs
|
|||||||
-- | When Hasql supports the COPY command then we can
|
-- | When Hasql supports the COPY command then we can
|
||||||
-- have a special payload just for CSV, but until
|
-- have a special payload just for CSV, but until
|
||||||
-- then CSV is converted to a JSON array.
|
-- then CSV is converted to a JSON array.
|
||||||
data Payload = PayloadJSON UniformObjects
|
data Payload = PayloadJSON UniformObjects deriving (Show, Eq)
|
||||||
| PayloadParseError ByteString
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
data Proxy = Proxy {
|
data Proxy = Proxy {
|
||||||
proxyScheme :: Text
|
proxyScheme :: Text
|
||||||
|
|||||||
Reference in New Issue
Block a user