feat: allow full response control when raising exceptions

This commit is contained in:
Taimoor Zaeem
2023-09-01 14:02:03 -05:00
committed by GitHub
parent 7dc6e2b899
commit 07fef25591
4 changed files with 207 additions and 4 deletions
+61
View File
@@ -1490,3 +1490,64 @@ spec actualPgVersion =
`shouldRespondWith`
[json| {"code":"22026","details":null,"hint":null,"message":"bit string length 6 does not match type bit(5)"} |]
{ matchStatus = 400 }
context "get message and details from raise sqlstate" $ do
it "gets message and details from raise sqlstate PGRST" $ do
r <- request methodGet "/rpc/raise_sqlstate_test1"
[] ""
let resStatus = simpleStatus r
resHeaders = simpleHeaders r
resBody = simpleBody r
liftIO $ do
resStatus `shouldBe` Status { statusCode = 332, statusMessage = "My Custom Status" }
resHeaders `shouldSatisfy` elem ("X-Header", "str")
resBody `shouldBe` [json|{"code":"123","message":"ABC","details":"DEF","hint":"XYZ"}|]
get "/rpc/raise_sqlstate_test2" `shouldRespondWith`
[json|{"code":"123","message":"ABC","details":null,"hint":null}|]
{ matchStatus = 332
, matchHeaders = ["X-Header" <:> "str"] }
it "get message and details from PGRST raise and checks standard status message" $ do
r <- request methodGet "/rpc/raise_sqlstate_test3"
[] ""
let resStatus = simpleStatus r
resHeaders = simpleHeaders r
resBody = simpleBody r
liftIO $ do
resStatus `shouldBe` Status { statusCode = 404, statusMessage = "Not Found" }
resHeaders `shouldSatisfy` elem ("X-Header", "str")
resBody `shouldBe` [json|{"code":"123","message":"ABC","details":null,"hint":null}|]
it "get message and details from PGRST raise and checks custom status message" $ do
r <- request methodGet "/rpc/raise_sqlstate_test4"
[] ""
let resStatus = simpleStatus r
resHeaders = simpleHeaders r
resBody = simpleBody r
liftIO $ do
resStatus `shouldBe` Status { statusCode = 404, statusMessage = "My Not Found" }
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" $
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" $
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" $
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 }