Skip to content

Commit

Permalink
refactor: rename JSONParseError to PGRSTParseError
Browse files Browse the repository at this point in the history
The error name is too generic otherwise.
  • Loading branch information
steve-chavez committed Oct 6, 2023
1 parent 4150042 commit 2252a8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/PostgREST/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ instance PgrstError PgError where

headers (PgError _ (SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ResultError (SQL.ServerError "PGRST" m d _ _p))))) =
case (parseMessage m, parseDetails d) of
(Just _, Just r) -> headers JSONParseError ++ map intoHeader (M.toList $ getHeaders r)
_ -> headers JSONParseError
(Just _, Just r) -> headers PGRSTParseError ++ map intoHeader (M.toList $ getHeaders r)
_ -> headers PGRSTParseError
where
intoHeader (k,v) = (CI.mk $ T.encodeUtf8 k, T.encodeUtf8 v)

Expand Down Expand Up @@ -409,7 +409,7 @@ instance JSON.ToJSON SQL.CommandError where
"message" .= getMessage r,
"details" .= checkMaybe (getDetails r),
"hint" .= checkMaybe (getHint r)]
_ -> JSON.toJSON JSONParseError
_ -> JSON.toJSON PGRSTParseError
where
checkMaybe = maybe JSON.Null JSON.String

Expand Down Expand Up @@ -473,7 +473,7 @@ pgErrorStatus authed (SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ResultError
"PGRST" ->
case (parseMessage m, parseDetails d) of
(Just _, Just r) -> maybe (toEnum $ getStatus r) (HTTP.mkStatus (getStatus r) . T.encodeUtf8) (getStatusText r)
_ -> status JSONParseError
_ -> status PGRSTParseError
_ -> HTTP.status400

_ -> HTTP.status500
Expand All @@ -491,7 +491,7 @@ data Error
| PgErr PgError
| PutMatchingPkError
| SingularityError Integer
| JSONParseError
| PGRSTParseError

instance PgrstError Error where
status (ApiRequestError err) = status err
Expand All @@ -505,7 +505,7 @@ instance PgrstError Error where
status (PgErr err) = status err
status PutMatchingPkError = HTTP.status400
status SingularityError{} = HTTP.status406
status JSONParseError = HTTP.status500
status PGRSTParseError = HTTP.status500

headers (ApiRequestError err) = headers err
headers (JwtTokenInvalid m) = [invalidTokenHeader m]
Expand Down Expand Up @@ -566,7 +566,7 @@ instance JSON.ToJSON Error where
"details" .= T.unwords ["The result contains", show n, "rows"],
"hint" .= JSON.Null]

toJSON JSONParseError = JSON.object [
toJSON PGRSTParseError = JSON.object [
"code" .= ApiRequestErrorCode21,
"message" .= ("The message and detail field of RAISE 'PGRST' error expects JSON" :: Text),
"details" .= JSON.Null,
Expand Down
6 changes: 3 additions & 3 deletions test/spec/Feature/Query/RpcSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,17 +1537,17 @@ spec actualPgVersion =
resHeaders `shouldSatisfy` elem ("X-Header", "str")
resBody `shouldBe` [json|{"code":"123","message":"ABC","details":null,"hint":null}|]

it "returns JSONParseError for invalid JSON in RAISE Message field" $
it "returns error for invalid JSON in RAISE Message field" $
get "/rpc/raise_sqlstate_invalid_json_message" `shouldRespondWith`
[json|{"code":"PGRST121","message":"The message and detail field of RAISE 'PGRST' error expects JSON","details":null,"hint":null}|]
{ matchStatus = 500 }

it "returns JSONParseError for invalid JSON in RAISE Details field" $
it "returns error for invalid JSON in RAISE Details field" $
get "/rpc/raise_sqlstate_invalid_json_details" `shouldRespondWith`
[json|{"code":"PGRST121","message":"The message and detail field of RAISE 'PGRST' error expects JSON","details":null,"hint":null}|]
{ matchStatus = 500 }

it "returns JSONParseError for missing Details field in RAISE" $
it "returns error for missing Details field in RAISE" $
get "/rpc/raise_sqlstate_missing_details" `shouldRespondWith`
[json|{"code":"PGRST121","message":"The message and detail field of RAISE 'PGRST' error expects JSON","details":null,"hint":null}|]
{ matchStatus = 500 }

0 comments on commit 2252a8e

Please sign in to comment.