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.

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:

Definition at line 41 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 375 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 382 of file rich_status.hpp.

◆ GetGoogleStatus() [1/2]

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

Definition at line 80 of file rich_status.hpp.

◆ GetGoogleStatus() [2/2]

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

Definition at line 79 of file rich_status.hpp.

◆ operator grpc::Status()

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

Definition at line 86 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:

Definition at line 389 of file rich_status.hpp.


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