Skip to content

Commit

Permalink
fix: fix wrong http status on pg error 42P17 infinite recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
taimoorzaeem authored and steve-chavez committed Apr 22, 2024
1 parent 80f83f0 commit 88abf60
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3361, Clarify PGRST204(column not found) error message - @steve-chavez
- #3373, Remove rejected mediatype `application/vnd.pgrst.object+json` from response - @taimoorzaeem
- #3418, Fix OpenAPI not tagging a FK column correctly on O2O relationships - @laurenceisla
- #3256, Fix wrong http status for pg error `42P17 infinite recursion` - @taimoorzaeem

### Deprecated

Expand Down
2 changes: 2 additions & 0 deletions docs/references/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ PostgREST translates `PostgreSQL error codes <https://www.postgresql.org/docs/cu
+--------------------------+-------------------------+---------------------------------+
| 42P01 | 404 | undefined table |
+--------------------------+-------------------------+---------------------------------+
| 42P17 | 500 | infinite recursion |
+--------------------------+-------------------------+---------------------------------+
| 42501 | | if authenticated 403, | insufficient privileges |
| | | else 401 | |
+--------------------------+-------------------------+---------------------------------+
Expand Down
1 change: 1 addition & 0 deletions src/PostgREST/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ pgErrorStatus authed (SQL.SessionUsageError (SQL.QueryError _ _ (SQL.ResultError
then HTTP.status406
else HTTP.status404 -- undefined function
"42P01" -> HTTP.status404 -- undefined table
"42P17" -> HTTP.status500 -- infinite recursion
"42501" -> if authed then HTTP.status403 else HTTP.status401 -- insufficient privilege
'P':'T':n -> fromMaybe HTTP.status500 (HTTP.mkStatus <$> readMaybe n <*> pure m)
"PGRST" ->
Expand Down
7 changes: 7 additions & 0 deletions test/spec/Feature/Query/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,10 @@ spec actualPgVersion = do
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson]
}

context "test infinite recursion error 42P17" $
it "return http status 500" $
get "/infinite_recursion?select=*" `shouldRespondWith`
[json|{"code":"42P17","message":"infinite recursion detected in rules for relation \"infinite_recursion\"","details":null,"hint":null}|]
{ matchStatus = 500 }

7 changes: 7 additions & 0 deletions test/spec/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3764,3 +3764,10 @@ create aggregate test.outfunc_agg (anyelement) (
create or replace function test.sleep(seconds double precision default 5) returns void as $$
select pg_sleep(seconds);
$$ language sql;

-- https://github.com/PostgREST/postgrest/issues/3256
create view test.infinite_recursion as
select * from test.projects;

create or replace view test.infinite_recursion as
select * from test.infinite_recursion;

0 comments on commit 88abf60

Please sign in to comment.