feat: add Proxy-Status header for better error response

This commit is contained in:
Taimoor Zaeem
2025-04-05 13:43:39 -05:00
committed by GitHub
parent 2811d6f997
commit f7f87b42ca
6 changed files with 102 additions and 5 deletions
+20
View File
@@ -1830,3 +1830,23 @@ def test_log_pool_req_observation(level, defaultenv):
else:
output = postgrest.read_stdout(nlines=4)
assert len(output) == 0
def test_proxy_status_header(defaultenv, metapostgrest):
"Test Proxy-Status header in statement timeout error"
role = "timeout_authenticator"
set_statement_timeout(metapostgrest, role, 1000) # 1 second
env = {
**defaultenv,
"PGUSER": role,
"PGRST_DB_ANON_ROLE": role,
}
with run(env=env) as postgrest:
response = postgrest.session.get("/rpc/sleep?seconds=2")
assert response.status_code == 500
assert response.headers["Proxy-Status"] == "PostgREST; error=57014"
data = response.json()
assert data["message"] == "canceling statement due to statement timeout"
+44 -1
View File
@@ -7,7 +7,8 @@ import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Protolude hiding (get)
import Protolude hiding (get)
import SpecHelper
pgErrorCodeMapping :: SpecWith ((), Application)
pgErrorCodeMapping = do
@@ -26,3 +27,45 @@ pgErrorCodeMapping = do
"hint": "Increase the configuration parameter \"max_stack_depth\" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.",
"message": "stack depth limit exceeded"}|]
{ matchStatus = 500 }
context "includes the proxy-status header on the response" $ do
it "works with ApiRequest error" $
get "/invalid/nested/paths"
`shouldRespondWith`
[json| {"code":"PGRST125","details":null,"hint":null,"message":"Invalid path specified in request URL"} |]
{ matchStatus = 404
, matchHeaders = ["Proxy-Status" <:> "PostgREST; error=PGRST125"]
}
it "works with SchemaCache error" $
get "/non_existent_table"
`shouldRespondWith`
[json| {"code":"PGRST205","details":null,"hint":"Perhaps you meant the table 'test.json_table'","message":"Could not find the table 'test.non_existent_table' in the schema cache"} |]
{ matchStatus = 404
, matchHeaders = ["Proxy-Status" <:> "PostgREST; error=PGRST205"]
}
it "works with Jwt error" $ do
let auth = authHeaderJWT "ey9zdGdyZXN0X3Rlc3RfYXV0aG9yIiwiaWQiOiJqZG9lIn0.y4vZuu1dDdwAl0-S00MCRWRYMlJ5YAMSir6Es6WtWx0"
request methodGet "/authors_only" [auth] ""
`shouldRespondWith`
[json| {"message":"Expected 3 parts in JWT; got 2","code":"PGRST301","hint":null,"details":null} |]
{ matchStatus = 401
, matchHeaders = ["Proxy-Status" <:> "PostgREST; error=PGRST301"]
}
it "works with raise sqlstate custom error" $
get "/rpc/raise_pt402"
`shouldRespondWith`
[json| {"code":"PT402","details":"Quota exceeded","hint":"Upgrade your plan","message":"Payment Required"} |]
{ matchStatus = 402
, matchHeaders = ["Proxy-Status" <:> "PostgREST; error=PT402"]
}
it "works with sqlstate PGRST custom error" $
get "/rpc/raise_sqlstate_test1"
`shouldRespondWith`
[json| {"code":"123","details":"DEF","hint":"XYZ","message":"ABC"} |]
{ matchStatus = 332
, matchHeaders = ["Proxy-Status" <:> "PostgREST; error=123"]
}