Skip to content

Commit c52fd3f

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
refactor: rename ApiRequestError type constructor to ApiRequestErr
Rename to keep the naming convention consistent with other type constructor names. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent 248b777 commit c52fd3f

6 files changed

Lines changed: 52 additions & 52 deletions

File tree

src/PostgREST/App.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache authResult@AuthRe
168168
timezones = dbTimezones sCache
169169
prefs = ApiRequest.userPreferences conf req timezones
170170

171-
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf prefs req body
171+
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestErr $ ApiRequest.userApiRequest conf prefs req body
172172
(planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache
173173

174174
let mainQ = Query.mainQuery plan conf apiReq authResult configDbPreRequest

src/PostgREST/Error.hs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -585,46 +585,46 @@ pgErrorStatus authed (SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ResultError
585585

586586

587587
instance PgrstError Error where
588-
status (ApiRequestError err) = status err
589-
status (SchemaCacheErr err) = status err
590-
status (JwtErr err) = status err
591-
status NoSchemaCacheError = HTTP.status503
592-
status (PgErr err) = status err
593-
594-
headers (ApiRequestError err) = headers err
595-
headers (SchemaCacheErr err) = headers err
596-
headers (JwtErr err) = headers err
597-
headers (PgErr err) = headers err
598-
headers NoSchemaCacheError = mempty
588+
status (ApiRequestErr err) = status err
589+
status (SchemaCacheErr err) = status err
590+
status (JwtErr err) = status err
591+
status NoSchemaCacheError = HTTP.status503
592+
status (PgErr err) = status err
593+
594+
headers (ApiRequestErr err) = headers err
595+
headers (SchemaCacheErr err) = headers err
596+
headers (JwtErr err) = headers err
597+
headers (PgErr err) = headers err
598+
headers NoSchemaCacheError = mempty
599599

600600
instance JSON.ToJSON Error where
601601
toJSON err = toJsonPgrstError
602602
(code err) (message err) (details err) (hint err)
603603

604604
instance ErrorBody Error where
605-
code (ApiRequestError err) = code err
606-
code (SchemaCacheErr err) = code err
607-
code (JwtErr err) = code err
608-
code NoSchemaCacheError = "PGRST002"
609-
code (PgErr err) = code err
605+
code (ApiRequestErr err) = code err
606+
code (SchemaCacheErr err) = code err
607+
code (JwtErr err) = code err
608+
code NoSchemaCacheError = "PGRST002"
609+
code (PgErr err) = code err
610610

611-
message (ApiRequestError err) = message err
611+
message (ApiRequestErr err) = message err
612612
message (SchemaCacheErr err) = message err
613613
message (JwtErr err) = message err
614614
message NoSchemaCacheError = "Could not query the database for the schema cache. Retrying."
615615
message (PgErr err) = message err
616616

617-
details (ApiRequestError err) = details err
618-
details (SchemaCacheErr err) = details err
619-
details (JwtErr err) = details err
620-
details NoSchemaCacheError = Nothing
621-
details (PgErr err) = details err
622-
623-
hint (ApiRequestError err) = hint err
624-
hint (SchemaCacheErr err) = hint err
625-
hint (JwtErr err) = hint err
626-
hint NoSchemaCacheError = Nothing
627-
hint (PgErr err) = hint err
617+
details (ApiRequestErr err) = details err
618+
details (SchemaCacheErr err) = details err
619+
details (JwtErr err) = details err
620+
details NoSchemaCacheError = Nothing
621+
details (PgErr err) = details err
622+
623+
hint (ApiRequestErr err) = hint err
624+
hint (SchemaCacheErr err) = hint err
625+
hint (JwtErr err) = hint err
626+
hint NoSchemaCacheError = Nothing
627+
hint (PgErr err) = hint err
628628

629629
instance PgrstError JwtError where
630630
status JwtDecodeErr{} = HTTP.unauthorized401

src/PostgREST/Error/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import PostgREST.SchemaCache.Routine (Routine (..))
2929
import Protolude
3030

3131
data Error
32-
= ApiRequestError ApiRequestError
32+
= ApiRequestErr ApiRequestError
3333
| SchemaCacheErr SchemaCacheError
3434
| JwtErr JwtError
3535
| NoSchemaCacheError

src/PostgREST/MainTx.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ failPut :: ResultSet -> DbHandler ()
222222
failPut RSStandard{rsQueryTotal=queryTotal} =
223223
when (queryTotal /= 1) $ do
224224
lift SQL.condemn
225-
throwError $ Error.ApiRequestError Error.PutMatchingPkError
225+
throwError $ Error.ApiRequestErr Error.PutMatchingPkError
226226

227227
-- |
228228
-- Fail a response if a single JSON object was requested and not exactly one
@@ -231,13 +231,13 @@ failNotSingular :: MediaType -> ResultSet -> DbHandler ()
231231
failNotSingular mediaType RSStandard{rsQueryTotal=queryTotal} =
232232
when (elem mediaType [MTVndSingularJSON True, MTVndSingularJSON False] && queryTotal /= 1) $ do
233233
lift SQL.condemn
234-
throwError $ Error.ApiRequestError . Error.SingularityError $ toInteger queryTotal
234+
throwError $ Error.ApiRequestErr . Error.SingularityError $ toInteger queryTotal
235235

236236
failExceedsMaxAffectedPref :: (Maybe PreferMaxAffected, Maybe PreferHandling) -> ResultSet -> DbHandler ()
237237
failExceedsMaxAffectedPref (Nothing,_) _ = pure ()
238238
failExceedsMaxAffectedPref (Just (PreferMaxAffected n), handling) RSStandard{rsQueryTotal=queryTotal} = when ((queryTotal > n) && (handling == Just Strict)) $ do
239239
lift SQL.condemn
240-
throwError $ Error.ApiRequestError . Error.MaxAffectedViolationError $ toInteger queryTotal
240+
throwError $ Error.ApiRequestErr . Error.MaxAffectedViolationError $ toInteger queryTotal
241241

242242
-- | Set a transaction to roll back if requested
243243
optionalRollback :: AppConfig -> ApiRequest -> DbHandler ()

src/PostgREST/Plan.hs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ wrappedReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest
172172
wrappedReadPlan identifier conf sCache apiRequest@ApiRequest{iPreferences=Preferences{..},..} headersOnly = do
173173
qi <- findTable identifier sCache
174174
rPlan <- readPlan qi conf sCache apiRequest
175-
(handler, mediaType) <- mapLeft ApiRequestError $ negotiateContent conf apiRequest qi iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
176-
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestError $ InvalidPreferences invalidPrefs else Right ()
175+
(handler, mediaType) <- mapLeft ApiRequestErr $ negotiateContent conf apiRequest qi iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
176+
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestErr $ InvalidPreferences invalidPrefs else Right ()
177177
return $ WrappedReadPlan rPlan SQL.Read handler mediaType headersOnly qi
178178

179179
mutateReadPlan :: Mutation -> ApiRequest -> QualifiedIdentifier -> AppConfig -> SchemaCache -> Either Error CrudPlan
180180
mutateReadPlan mutation apiRequest@ApiRequest{iPreferences=Preferences{..},..} identifier conf sCache = do
181181
qi <- findTable identifier sCache
182182
rPlan <- readPlan qi conf sCache apiRequest
183183
mPlan <- mutatePlan mutation qi apiRequest sCache rPlan
184-
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestError $ InvalidPreferences invalidPrefs else Right ()
185-
(handler, mediaType) <- mapLeft ApiRequestError $ negotiateContent conf apiRequest qi iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
184+
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestErr $ InvalidPreferences invalidPrefs else Right ()
185+
(handler, mediaType) <- mapLeft ApiRequestErr $ negotiateContent conf apiRequest qi iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
186186
return $ MutateReadPlan rPlan mPlan SQL.Write handler mediaType mutation qi
187187

188188
callReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> InvokeMethod -> Either Error CrudPlan
@@ -204,15 +204,15 @@ callReadPlan identifier conf sCache apiRequest@ApiRequest{iPreferences=Preferenc
204204
(Inv, Routine.Immutable) -> SQL.Read
205205
(Inv, Routine.Volatile) -> SQL.Write
206206
cPlan = callPlan proc apiRequest paramKeys args rPlan
207-
(handler, mediaType) <- mapLeft ApiRequestError $ negotiateContent conf apiRequest relIdentifier iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
208-
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestError $ InvalidPreferences invalidPrefs else Right ()
207+
(handler, mediaType) <- mapLeft ApiRequestErr $ negotiateContent conf apiRequest relIdentifier iAcceptMediaType (dbMediaHandlers sCache) (hasDefaultSelect rPlan)
208+
if not (null invalidPrefs) && preferHandling == Just Strict then Left $ ApiRequestErr $ InvalidPreferences invalidPrefs else Right ()
209209
failMaxAffectedRpcReturnsSingle (preferMaxAffected, preferHandling) proc
210210
return $ CallReadPlan rPlan cPlan txMode proc handler mediaType invMethod identifier
211211
where
212212
qsParams' = QueryParams.qsParams iQueryParams
213213

214214
failMaxAffectedRpcReturnsSingle :: (Maybe PreferMaxAffected, Maybe PreferHandling) -> Routine -> Either Error ()
215-
failMaxAffectedRpcReturnsSingle (Just (PreferMaxAffected _), Just Strict) rout = if funcReturnsSingle rout then Left $ ApiRequestError MaxAffectedRpcViolation else Right ()
215+
failMaxAffectedRpcReturnsSingle (Just (PreferMaxAffected _), Just Strict) rout = if funcReturnsSingle rout then Left $ ApiRequestErr MaxAffectedRpcViolation else Right ()
216216
failMaxAffectedRpcReturnsSingle _ _ = Right ()
217217

218218
hasDefaultSelect :: ReadPlanTree -> Bool
@@ -225,7 +225,7 @@ inspectPlan apiRequest headersOnly schema = do
225225
accepts = iAcceptMediaType apiRequest
226226
mediaType <- if not . null $ L.intersect accepts producedMTs
227227
then Right MTOpenAPI
228-
else Left . ApiRequestError . MediaTypeError $ MediaType.toMime <$> accepts
228+
else Left . ApiRequestErr . MediaTypeError $ MediaType.toMime <$> accepts
229229
return $ InspectPlan mediaType SQL.Read headersOnly schema
230230

231231
{-|
@@ -784,7 +784,7 @@ hoistIntoRelSelectFields _ r = r
784784
-- to order once it's aggregated if it's not selected in the inner query beforehand.
785785
addToManyOrderSelects :: ReadPlanTree -> Either Error ReadPlanTree
786786
addToManyOrderSelects (Node rp@ReadPlan{order, select, relAggAlias, relSelect, relSpread = Just ToManySpread {}} forest)
787-
| anyAggSel || anyAggRelSel = Left $ ApiRequestError $ NotImplemented "Aggregates are not implemented for one-to-many or many-to-many spreads."
787+
| anyAggSel || anyAggRelSel = Left $ ApiRequestErr $ NotImplemented "Aggregates are not implemented for one-to-many or many-to-many spreads."
788788
| otherwise = Node rp { order = [], relSpread = newRelSpread } <$> addToManyOrderSelects `traverse` forest
789789
where
790790
newRelSpread = Just ToManySpread { stExtraSelect = addSprExtraSelects, stOrder = addSprOrder}
@@ -806,7 +806,7 @@ addToManyOrderSelects (Node rp forest) = Node rp <$> addToManyOrderSelects `trav
806806

807807
validateAggFunctions :: Bool -> ReadPlanTree -> Either Error ReadPlanTree
808808
validateAggFunctions aggFunctionsAllowed (Node rp@ReadPlan {select} forest)
809-
| not aggFunctionsAllowed && any (isJust . csAggFunction) select = Left $ ApiRequestError AggregatesNotAllowed
809+
| not aggFunctionsAllowed && any (isJust . csAggFunction) select = Left $ ApiRequestErr AggregatesNotAllowed
810810
| otherwise = Node rp <$> traverse (validateAggFunctions aggFunctionsAllowed) forest
811811

812812
-- | Lookup table in the schema cache before creating read plan
@@ -860,9 +860,9 @@ addRelatedOrders (Node rp@ReadPlan{order,from} forest) = do
860860
name = fromMaybe relName relAlias in
861861
if isToOne == Just True
862862
then Right $ cot{coRelation=relAggAlias}
863-
else Left $ ApiRequestError $ RelatedOrderNotToOne (qiName from) name
863+
else Left $ ApiRequestErr $ RelatedOrderNotToOne (qiName from) name
864864
Nothing ->
865-
Left $ ApiRequestError $ NotEmbedded coRelation
865+
Left $ ApiRequestErr $ NotEmbedded coRelation
866866

867867
-- | Searches for null filters on embeds, e.g. `projects=not.is.null` on `GET /clients?select=*,projects(*)&projects=not.is.null`
868868
--
@@ -956,7 +956,7 @@ addRanges ApiRequest{..} rReq =
956956
_ -> foldr addRangeToNode (Right rReq) =<< ranges
957957
where
958958
ranges :: Either Error [(EmbedPath, NonnegRange)]
959-
ranges = first (ApiRequestError . QueryParamError) $ QueryParams.pRequestRange `traverse` HM.toList iRange
959+
ranges = first (ApiRequestErr . QueryParamError) $ QueryParams.pRequestRange `traverse` HM.toList iRange
960960

961961
addRangeToNode :: (EmbedPath, NonnegRange) -> Either Error ReadPlanTree -> Either Error ReadPlanTree
962962
addRangeToNode = updateNode (\r (Node q f) -> Node q{range_=r} f)
@@ -990,7 +990,7 @@ updateNode f ([], a) rr = f a <$> rr
990990
updateNode _ _ (Left e) = Left e
991991
updateNode f (targetNodeName:remainingPath, a) (Right (Node rootNode forest)) =
992992
case findNode of
993-
Nothing -> Left $ ApiRequestError $ NotEmbedded targetNodeName
993+
Nothing -> Left $ ApiRequestErr $ NotEmbedded targetNodeName
994994
Just target ->
995995
(\node -> Node rootNode $ node : delete target forest) <$>
996996
updateNode f (remainingPath, a) (Right target)
@@ -1014,7 +1014,7 @@ mutatePlan mutation qi ApiRequest{iPreferences=Preferences{..}, ..} SchemaCache{
10141014
_ -> False) qsFiltersRoot
10151015
then mapRight (\typedColumns -> Insert qi typedColumns body (Just (MergeDuplicates, pkCols)) combinedLogic returnings mempty False) typedColumnsOrError
10161016
else
1017-
Left $ ApiRequestError InvalidFilters
1017+
Left $ ApiRequestErr InvalidFilters
10181018
MutationDelete -> Right $ Delete qi combinedLogic returnings
10191019
where
10201020
ctx = ResolverContext dbTables dbRepresentations qi "json"

src/PostgREST/Response.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ actionResponse (DbCrudResult plan@WrappedReadPlan{pMedia, wrHdrsOnly=headersOnly
7979
++ cLHeader
8080
++ contentTypeHeaders pMedia ctxApiRequest
8181
++ prefHeader
82-
bod | status == HTTP.status416 = Error.errorPayload $ Error.ApiRequestError $ Error.InvalidRange $
82+
bod | status == HTTP.status416 = Error.errorPayload $ Error.ApiRequestErr $ Error.InvalidRange $
8383
Error.OutOfBounds (show $ RangeQuery.rangeOffset iTopLevelRange) (maybe "0" show rsTableTotal)
8484
| headersOnly = mempty
8585
| otherwise = LBS.fromStrict rsBody
@@ -183,7 +183,7 @@ actionResponse (DbCrudResult plan@CallReadPlan{pMedia, crInvMthd=invMethod, crPr
183183
(status, contentRange) =
184184
RangeQuery.rangeStatusHeader iTopLevelRange rsQueryTotal rsTableTotal
185185
rsOrErrBody = if status == HTTP.status416
186-
then Error.errorPayload $ Error.ApiRequestError $ Error.InvalidRange
186+
then Error.errorPayload $ Error.ApiRequestErr $ Error.InvalidRange
187187
$ Error.OutOfBounds (show $ RangeQuery.rangeOffset iTopLevelRange) (maybe "0" show rsTableTotal)
188188
else LBS.fromStrict rsBody
189189
isHeadMethod = invMethod == InvRead True
@@ -247,11 +247,11 @@ overrideStatusHeaders rsGucStatus rsGucHeaders pgrstStatus pgrstHeaders = do
247247

248248
decodeGucHeaders :: Maybe BS.ByteString -> Either Error.Error [GucHeader]
249249
decodeGucHeaders =
250-
maybe (Right []) $ first (const . Error.ApiRequestError $ Error.GucHeadersError) . JSON.eitherDecode . LBS.fromStrict
250+
maybe (Right []) $ first (const . Error.ApiRequestErr $ Error.GucHeadersError) . JSON.eitherDecode . LBS.fromStrict
251251

252252
decodeGucStatus :: Maybe Text -> Either Error.Error (Maybe HTTP.Status)
253253
decodeGucStatus =
254-
maybe (Right Nothing) $ first (const . Error.ApiRequestError $ Error.GucStatusError) . fmap (Just . toEnum . fst) . decimal
254+
maybe (Right Nothing) $ first (const . Error.ApiRequestErr $ Error.GucStatusError) . fmap (Just . toEnum . fst) . decimal
255255

256256
contentLengthHeader :: LBS.ByteString -> HTTP.Header
257257
contentLengthHeader body = ("Content-Length", show (LBS.length body))

0 commit comments

Comments
 (0)