feat: allow full response control when raising exceptions
This commit is contained in:
@@ -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 }
|
||||
|
||||
Vendored
+69
@@ -3368,3 +3368,72 @@ $$ language sql;
|
||||
create function returns_setof_record_params(id int, name text) returns setof record as $$
|
||||
select * from projects p where p.id >= $1 and p.name like $2;
|
||||
$$ language sql;
|
||||
|
||||
create function raise_sqlstate_test1() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC","details":"DEF","hint":"XYZ"}',
|
||||
detail = '{"status":332,"status_text":"My Custom Status","headers":{"X-Header":"str"}}';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_test2() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC"}',
|
||||
detail = '{"status":332,"headers":{"X-Header":"str"}}';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_test3() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC"}',
|
||||
detail = '{"status":404,"headers":{"X-Header":"str"}}';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_test4() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC"}',
|
||||
detail = '{"status":404,"status_text":"My Not Found","headers":{"X-Header":"str"}}';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_invalid_json_message() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = 'INVALID',
|
||||
detail = '{"status":332,"headers":{"X-Header":"str"}}';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_invalid_json_details() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC","details":"DEF"}',
|
||||
detail = 'INVALID';
|
||||
end
|
||||
$$;
|
||||
|
||||
create function raise_sqlstate_missing_details() returns void
|
||||
language plpgsql
|
||||
as $$
|
||||
begin
|
||||
raise sqlstate 'PGRST' USING
|
||||
message = '{"code":"123","message":"ABC","details":"DEF"}';
|
||||
end
|
||||
$$;
|
||||
|
||||
Reference in New Issue
Block a user