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_EXPIRED` 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} // namespace ugrpc
39
40namespace formats::parse {
41
42/// @ref yaml_config::YamlConfig parsing support for `grpc::StatusCode`.
43grpc::StatusCode Parse(const yaml_config::YamlConfig& value, To<grpc::StatusCode>);
44
45/// Support for parsing `grpc::StatusCode` from string. Used for headers and map keys.
46grpc::StatusCode Parse(std::string_view value, To<grpc::StatusCode>);
47
48} // namespace formats::parse
49
50namespace formats::serialize {
51
52formats::json::Value Serialize(const grpc::StatusCode& value, formats::serialize::To<formats::json::Value>);
53
54}
55
56USERVER_NAMESPACE_END