Skip to content

Commit

Permalink
(svc) catch not found errors when retrieving access policies
Browse files Browse the repository at this point in the history
  • Loading branch information
uatuko committed Aug 7, 2023
1 parent d0cc84a commit 98e82da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/svc/access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,16 @@ grpc::ServerUnaryReactor *Access::RetrievePolicy(
gk::v1::AccessPolicy *response) {
auto *reactor = context->DefaultReactor();

// TODO: error handling
auto policy = datastore::RetrieveAccessPolicy(request->id());
map(policy, response);
try {
auto policy = datastore::RetrieveAccessPolicy(request->id());
map(policy, response);
} catch (const err::DatastoreAccessPolicyNotFound &) {
reactor->Finish(grpc::Status(grpc::StatusCode::NOT_FOUND, "Policy not found"));
return reactor;
} catch (...) {
reactor->Finish(grpc::Status(grpc::StatusCode::UNAVAILABLE, "Failed to retrieve data"));
return reactor;
}

reactor->Finish(grpc::Status::OK);
return reactor;
Expand Down
15 changes: 15 additions & 0 deletions src/svc/access_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,19 @@ TEST_F(svc_AccessTest, RetrievePolicy) {
google::protobuf::util::MessageToJsonString(response.rules(0).attrs(), &attrs);
EXPECT_EQ(policy.rules().cbegin()->attrs, attrs);
}

// Error: policy not found
{
grpc::CallbackServerContext ctx;
grpc::testing::DefaultReactorTestPeer peer(&ctx);
gk::v1::AccessPolicy response;

gk::v1::AccessRetrievePolicyRequest request;
request.set_id("id:svc_AccessTest.RetrievePolicy-not_found");

auto reactor = svc.RetrievePolicy(&ctx, &request, &response);
EXPECT_TRUE(peer.test_status_set());
EXPECT_EQ(grpc::StatusCode::NOT_FOUND, peer.test_status().error_code());
EXPECT_EQ("Policy not found", peer.test_status().error_message());
}
}

0 comments on commit 98e82da

Please sign in to comment.