1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/transport/metadata.h"
16
17 #include <grpc/support/port_platform.h>
18
19 #include "src/core/lib/transport/error_utils.h"
20 #include "src/core/lib/transport/metadata_batch.h"
21
22 namespace grpc_core {
23
ServerMetadataFromStatus(const absl::Status & status)24 ServerMetadataHandle ServerMetadataFromStatus(const absl::Status& status) {
25 auto hdl = Arena::MakePooledForOverwrite<ServerMetadata>();
26 grpc_status_code code;
27 std::string message;
28 grpc_error_get_status(status, Timestamp::InfFuture(), &code, &message,
29 nullptr, nullptr);
30 hdl->Set(GrpcStatusMetadata(), code);
31 if (!status.ok()) {
32 hdl->Set(GrpcMessageMetadata(), Slice::FromCopiedString(message));
33 }
34 return hdl;
35 }
36
CancelledServerMetadataFromStatus(const absl::Status & status)37 ServerMetadataHandle CancelledServerMetadataFromStatus(
38 const absl::Status& status) {
39 auto hdl = ServerMetadataFromStatus(status);
40 hdl->Set(GrpcCallWasCancelled(), true);
41 return hdl;
42 }
43
ServerMetadataFromStatus(grpc_status_code code,absl::string_view message)44 ServerMetadataHandle ServerMetadataFromStatus(grpc_status_code code,
45 absl::string_view message) {
46 auto hdl = Arena::MakePooledForOverwrite<ServerMetadata>();
47 hdl->Set(GrpcStatusMetadata(), code);
48 hdl->Set(GrpcMessageMetadata(), Slice::FromCopiedString(message));
49 return hdl;
50 }
51
CancelledServerMetadataFromStatus(grpc_status_code code,absl::string_view message)52 ServerMetadataHandle CancelledServerMetadataFromStatus(
53 grpc_status_code code, absl::string_view message) {
54 auto hdl = Arena::MakePooledForOverwrite<ServerMetadata>();
55 hdl->Set(GrpcStatusMetadata(), code);
56 hdl->Set(GrpcMessageMetadata(), Slice::FromCopiedString(message));
57 hdl->Set(GrpcCallWasCancelled(), true);
58 return hdl;
59 }
60
61 } // namespace grpc_core
62