Skip to content

Commit

Permalink
Merge pull request #120
Browse files Browse the repository at this point in the history
Fix to return correct gRPC errors when creating duplicate relations
  • Loading branch information
uatuko authored Dec 19, 2024
2 parents d00df95 + 280bf6c commit 9b3f524
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/svc/relations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ google::rpc::Status Impl::exception() noexcept {

try {
std::rethrow_exception(std::current_exception());
} catch (const err::DbTupleAlreadyExists &e) {
status.set_code(google::rpc::ALREADY_EXISTS);
status.set_message(std::string(e.str()));
} catch (const err::DbTupleInvalidData &e) {
status.set_code(google::rpc::INVALID_ARGUMENT);
status.set_message(std::string(e.str()));
Expand Down
33 changes: 33 additions & 0 deletions src/svc/relations_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,39 @@ TEST_F(svc_RelationsTest, Create) {

EXPECT_FALSE(result.response);
}

// Error: duplicate relation
{
db::Tuple tuple({
.lEntityId = "left",
.lEntityType = "svc_RelationsTest.Create-duplicate",
.relation = "relation",
.rEntityId = "right",
.rEntityType = "svc_RelationsTest.Create-duplicate",
});
ASSERT_NO_THROW(tuple.store());

rpcCreate::request_type request;

auto *left = request.mutable_left_entity();
left->set_id(tuple.lEntityId());
left->set_type(tuple.lEntityType());

request.set_relation(tuple.relation());

auto *right = request.mutable_right_entity();
right->set_id(tuple.rEntityId());
right->set_type(tuple.rEntityType());

rpcCreate::result_type result;
EXPECT_NO_THROW(result = svc.call<rpcCreate>(ctx, request));

EXPECT_EQ(grpcxx::status::code_t::already_exists, result.status.code());
EXPECT_EQ(
"CAYSJVtydWVrOjEuNC40LjQwOV0gVHVwbGUgYWxyZWFkeSBleGlzdHM=", result.status.details());

EXPECT_FALSE(result.response);
}
}

TEST_F(svc_RelationsTest, Delete) {
Expand Down

0 comments on commit 9b3f524

Please sign in to comment.