userver: ugrpc::RichStatus Class Reference
Loading...
Searching...
No Matches
ugrpc::RichStatus Class Referencefinal

#include <userver/ugrpc/rich_status.hpp>

Detailed Description

A wrapper around google::rpc::Status that provides a convenient API for creating and managing gRPC status responses with rich error details.

Documentation:

See also
RichStatus

RichStatus allows you to create gRPC status responses with structured error information conforming to the Google RPC error model. It supports adding multiple error details of various types to provide comprehensive error information to clients.

See also
https://google.aip.dev/193
https://grpc.io/docs/guides/error/
ugrpc::ErrorInfo
ugrpc::RetryInfo
ugrpc::DebugInfo
ugrpc::QuotaFailure
ugrpc::PreconditionFailure
ugrpc::BadRequest
ugrpc::RequestInfo
ugrpc::ResourceInfo
ugrpc::Help
ugrpc::LocalizedMessage

Example usage:

SayHelloResult SayHello(CallContext& /*context*/, sample::ugrpc::GreetingRequest&& /*request*/) override {
ugrpc::RichStatus rich_status{
grpc::StatusCode::RESOURCE_EXHAUSTED,
"message",
ugrpc::Help{{{"test_url", "http://help.url/auth/fts-documentation/tvm"}}},
ugrpc::QuotaFailure{{{"123-pipepline000", "fts quota [fts-receive] exhausted"}}},
};
return rich_status.ToGrpcStatus();
}

Definition at line 43 of file rich_status.hpp.

Public Member Functions

 RichStatus ()=default
 Constructs an OK status with no error details.
 
 RichStatus (const grpc::Status &grpc_status)
 Constructs a RichStatus from a grpc::Status.
 
template<typename... TDetails>
 RichStatus (grpc::StatusCode code, std::string message, TDetails &&... details)
 Constructs a RichStatus with the given code, message, and details.
 
template<typename TDetail>
RichStatusAddDetail (TDetail &&detail)
 Adds an error detail to the status.
 
const google::rpc::Status & GetGoogleStatus () const &
 
google::rpc::Status GetGoogleStatus () &&
 
grpc::Status ToGrpcStatus () const
 Converts this RichStatus to a grpc::Status.
 
 operator grpc::Status () const
 

Static Public Member Functions

template<typename TRichErrorDetail>
static std::optional< TRichErrorDetail > TryGetDetail (const google::rpc::Status &status)
 Attempts to extract a specific rich error detail from google::rpc::Status
 

Constructor & Destructor Documentation

◆ RichStatus() [1/2]

ugrpc::RichStatus::RichStatus ( const grpc::Status & grpc_status)
explicit

Constructs a RichStatus from a grpc::Status.

Parameters
grpc_statusThe gRPC status to convert. If it contains serialized google::rpc::Status in error_details(), it will be parsed and used.

◆ RichStatus() [2/2]

template<typename... TDetails>
ugrpc::RichStatus::RichStatus ( grpc::StatusCode code,
std::string message,
TDetails &&... details )

Constructs a RichStatus with the given code, message, and details.

Parameters
codeThe gRPC status code
messageThe error message
detailsError details conforming to google.rpc.error_details types. Must have a ToGoogleErrorDetail() method.

Definition at line 377 of file rich_status.hpp.

Member Function Documentation

◆ AddDetail()

template<typename TDetail>
RichStatus & ugrpc::RichStatus::AddDetail ( TDetail && detail)

Adds an error detail to the status.

Parameters
detailThe error detail to add. Must have a ToGoogleErrorDetail() method.
Returns
Reference to this RichStatus for method chaining.

Definition at line 384 of file rich_status.hpp.

◆ GetGoogleStatus() [1/2]

google::rpc::Status ugrpc::RichStatus::GetGoogleStatus ( ) &&
inline

Definition at line 82 of file rich_status.hpp.

◆ GetGoogleStatus() [2/2]

const google::rpc::Status & ugrpc::RichStatus::GetGoogleStatus ( ) const &
inlinenodiscard

Definition at line 81 of file rich_status.hpp.

◆ operator grpc::Status()

ugrpc::RichStatus::operator grpc::Status ( ) const
inlineexplicitnodiscard

Definition at line 88 of file rich_status.hpp.

◆ ToGrpcStatus()

grpc::Status ugrpc::RichStatus::ToGrpcStatus ( ) const
nodiscard

Converts this RichStatus to a grpc::Status.

Returns
A grpc::Status with serialized error details.

◆ TryGetDetail()

template<typename TRichErrorDetail>
std::optional< TRichErrorDetail > ugrpc::RichStatus::TryGetDetail ( const google::rpc::Status & status)
staticnodiscard

Attempts to extract a specific rich error detail from google::rpc::Status

Returns
The extracted rich error detail if found, std::nullopt otherwise

According to AIP-193 (https://google.aip.dev/193), each status should contain at most one detail of each type. If multiple details of the same type exist (violating AIP-193), this function will return only the first encountered instance.

Example usage:

UTEST_F(GrpcClientWithDetailedRichErrorTest, TryGetErrorDetail) {
sample::ugrpc::GreetingRequest out;
out.set_name("userver");
try {
GetClient().SayHello(out);
FAIL() << "Expected ResourceExhaustedError";
const auto& status = e.GetStatus();
auto gstatus = ugrpc::ToGoogleRpcStatus(status);
EXPECT_TRUE(gstatus.has_value());
const auto help_detail = ugrpc::RichStatus::TryGetDetail<ugrpc::Help>(*gstatus);
EXPECT_TRUE(help_detail.has_value());
EXPECT_EQ(help_detail->links.size(), 1);
EXPECT_EQ(help_detail->links[0].description, "test_url");
EXPECT_EQ(help_detail->links[0].url, "http://help.url/auth/fts-documentation/tvm");
const auto quota_failure_detail = ugrpc::RichStatus::TryGetDetail<ugrpc::QuotaFailure>(*gstatus);
EXPECT_TRUE(quota_failure_detail.has_value());
EXPECT_EQ(quota_failure_detail->violations.size(), 1);
EXPECT_EQ(quota_failure_detail->violations[0].subject, "123-pipepline000");
EXPECT_EQ(quota_failure_detail->violations[0].description, "fts quota [fts-receive] exhausted");
}
}

Definition at line 391 of file rich_status.hpp.


The documentation for this class was generated from the following file: