userver: userver/ugrpc/status_codes.hpp Source File
Loading...
Searching...
No Matches
status_codes.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/status_codes.hpp
4/// @brief Utilities for grpc::StatusCode
5
6#include <grpcpp/support/status.h>
7
8#include <userver/formats/json_fwd.hpp>
9#include <userver/yaml_config/fwd.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace ugrpc {
14
15/// @brief Convert string to grpc::StatusCode
16/// @throws std::runtime_error
17grpc::StatusCode StatusCodeFromString(std::string_view str);
18
19/// @brief Convert grpc::StatusCode to string
20std::string ToString(grpc::StatusCode code) noexcept;
21
22/// @brief Whether a given status code is definitely a server-side error
23///
24/// Currently includes:
25///
26/// * `UNKNOWN`
27/// * `UNIMPLEMENTED`
28/// * `INTERNAL`
29/// * `UNAVAILABLE`
30/// * `DATA_LOSS`
31///
32/// We intentionally do not include `CANCELLED` and `DEADLINE_EXCEEDED` here, because the situation may either be
33/// considered not erroneous at all (when a client explicitly cancels an RPC; when a client attempts an RPC with a very
34/// short deadline), or there is no single obvious service to blame (when the collective deadline expires for an RPC
35/// tree).
36bool IsServerError(grpc::StatusCode code) noexcept;
37
38/// @brief Check if a gRPC status code is retryable
39///
40/// Returns true for status codes that typically indicate transient failures
41/// and are safe to retry:
42///
43/// * `CANCELLED`
44/// * `UNKNOWN`
45/// * `DEADLINE_EXCEEDED`
46/// * `INTERNAL`
47/// * `UNAVAILABLE`
48///
49/// @param code The gRPC status code to check
50/// @return true if the status code is retryable, false otherwise
51bool IsRetryable(grpc::StatusCode code) noexcept;
52
53} // namespace ugrpc
54
55namespace formats::parse {
56
57/// @ref yaml_config::YamlConfig parsing support for `grpc::StatusCode`.
58grpc::StatusCode Parse(const yaml_config::YamlConfig& value, To<grpc::StatusCode>);
59
60/// Support for parsing `grpc::StatusCode` from string. Used for headers and map keys.
61grpc::StatusCode Parse(std::string_view value, To<grpc::StatusCode>);
62
63} // namespace formats::parse
64
65namespace formats::serialize {
66
67formats::json::Value Serialize(const grpc::StatusCode& value, formats::serialize::To<formats::json::Value>);
68
69}
70
71USERVER_NAMESPACE_END