aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/grpc/src/core/lib/gprpp/status_helper.h
blob: e3a14ee09c4ed6f0bcfbf60ecdb0a71f622fc720 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//
// Copyright 2021 the gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

#ifndef GRPC_SRC_CORE_LIB_GPRPP_STATUS_HELPER_H
#define GRPC_SRC_CORE_LIB_GPRPP_STATUS_HELPER_H

#include <grpc/support/port_platform.h>

#include <stdint.h>

#include <util/generic/string.h>
#include <util/string/cast.h>
#include <vector>

#include "y_absl/status/status.h"
#include "y_absl/strings/string_view.h"
#include "y_absl/time/time.h"
#include "y_absl/types/optional.h"

#include "src/core/lib/gprpp/debug_location.h"

extern "C" {
struct google_rpc_Status;
struct upb_Arena;
}

#define GRPC_RETURN_IF_ERROR(expr)      \
  do {                                  \
    const y_absl::Status status = (expr); \
    if (!status.ok()) return status;    \
  } while (0)

namespace grpc_core {

/// This enum should have the same value of grpc_error_ints
enum class StatusIntProperty {
  /// 'errno' from the operating system
  kErrorNo,
  /// __LINE__ from the call site creating the error
  kFileLine,
  /// stream identifier: for errors that are associated with an individual
  /// wire stream
  kStreamId,
  /// grpc status code representing this error
  // TODO(veblush): Remove this after grpc_error is replaced with y_absl::Status
  kRpcStatus,
  /// offset into some binary blob (usually represented by
  /// RAW_BYTES) where the error occurred
  kOffset,
  /// context sensitive index associated with the error
  kIndex,
  /// context sensitive size associated with the error
  kSize,
  /// http2 error code associated with the error (see the HTTP2 RFC)
  kHttp2Error,
  /// TSI status code associated with the error
  kTsiCode,
  /// WSAGetLastError() reported when this error occurred
  kWsaError,
  /// File descriptor associated with this error
  kFd,
  /// HTTP status (i.e. 404)
  kHttpStatus,
  /// chttp2: did the error occur while a write was in progress
  kOccurredDuringWrite,
  /// channel connectivity state associated with the error
  ChannelConnectivityState,
  /// LB policy drop
  kLbPolicyDrop,
};

/// This enum should have the same value of grpc_error_strs
enum class StatusStrProperty {
  /// top-level textual description of this error
  kDescription,
  /// source file in which this error occurred
  kFile,
  /// operating system description of this error
  kOsError,
  /// syscall that generated this error
  kSyscall,
  /// peer that we were trying to communicate when this error occurred
  kTargetAddress,
  /// grpc status message associated with this error
  kGrpcMessage,
  /// hex dump (or similar) with the data that generated this error
  kRawBytes,
  /// tsi error string associated with this error
  kTsiError,
  /// filename that we were trying to read/write when this error occurred
  kFilename,
  /// key associated with the error
  kKey,
  /// value associated with the error
  kValue,
};

/// This enum should have the same value of grpc_error_times
enum class StatusTimeProperty {
  /// timestamp of error creation
  kCreated,
};

/// Creates a status with given additional information
y_absl::Status StatusCreate(
    y_absl::StatusCode code, y_absl::string_view msg, const DebugLocation& location,
    std::vector<y_absl::Status> children) GRPC_MUST_USE_RESULT;

/// Sets the int property to the status
void StatusSetInt(y_absl::Status* status, StatusIntProperty key, intptr_t value);

/// Gets the int property from the status
y_absl::optional<intptr_t> StatusGetInt(
    const y_absl::Status& status, StatusIntProperty key) GRPC_MUST_USE_RESULT;

/// Sets the str property to the status
void StatusSetStr(y_absl::Status* status, StatusStrProperty key,
                  y_absl::string_view value);

/// Gets the str property from the status
y_absl::optional<TString> StatusGetStr(
    const y_absl::Status& status, StatusStrProperty key) GRPC_MUST_USE_RESULT;

/// Sets the time property to the status
void StatusSetTime(y_absl::Status* status, StatusTimeProperty key,
                   y_absl::Time time);

/// Gets the time property from the status
y_absl::optional<y_absl::Time> StatusGetTime(
    const y_absl::Status& status, StatusTimeProperty key) GRPC_MUST_USE_RESULT;

/// Adds a child status to status
void StatusAddChild(y_absl::Status* status, y_absl::Status child);

/// Returns all children status from a status
std::vector<y_absl::Status> StatusGetChildren(y_absl::Status status)
    GRPC_MUST_USE_RESULT;

/// Returns a string representation from status
/// Error status will be like
///   STATUS[:MESSAGE] [{PAYLOADS[, children:[CHILDREN-STATUS-LISTS]]}]
/// e.g.
///   CANCELLATION:SampleMessage {errno:'2021', line:'54', children:[ABORTED]}
TString StatusToString(const y_absl::Status& status) GRPC_MUST_USE_RESULT;

namespace internal {

/// Builds a upb message, google_rpc_Status from a status
/// This is for internal implementation & test only
google_rpc_Status* StatusToProto(const y_absl::Status& status,
                                 upb_Arena* arena) GRPC_MUST_USE_RESULT;

/// Builds a status from a upb message, google_rpc_Status
/// This is for internal implementation & test only
y_absl::Status StatusFromProto(google_rpc_Status* msg) GRPC_MUST_USE_RESULT;

/// Returns ptr that is allocated in the heap memory and the given status is
/// copied into. This ptr can be used to get Status later and should be
/// freed by StatusFreeHeapPtr. This can be 0 in case of OkStatus.
uintptr_t StatusAllocHeapPtr(y_absl::Status s);

/// Frees the allocated status at heap ptr.
void StatusFreeHeapPtr(uintptr_t ptr);

/// Get the status from a heap ptr.
y_absl::Status StatusGetFromHeapPtr(uintptr_t ptr);

/// Move the status from a heap ptr. (GetFrom & FreeHeap)
y_absl::Status StatusMoveFromHeapPtr(uintptr_t ptr);

}  // namespace internal

}  // namespace grpc_core

#endif  // GRPC_SRC_CORE_LIB_GPRPP_STATUS_HELPER_H