xref: /aosp_15_r20/external/grpc-grpc/src/ruby/lib/grpc/google_rpc_status_utils.rb (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2017 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
15require_relative './structs'
16
17# GRPC contains the General RPC module.
18module GRPC
19  # GoogleRpcStatusUtils provides utilities to convert between a
20  # GRPC::Core::Status and a deserialized Google::Rpc::Status proto
21  # Returns nil if the grpc-status-details-bin trailer could not be
22  # converted to a GoogleRpcStatus due to the server not providing
23  # the necessary trailers.
24  # Raises an error if the server did provide the necessary trailers
25  # but they fail to deseriliaze into a GoogleRpcStatus protobuf.
26  class GoogleRpcStatusUtils
27    def self.extract_google_rpc_status(status)
28      fail ArgumentError, 'bad type' unless status.is_a? Struct::Status
29      grpc_status_details_bin_trailer = 'grpc-status-details-bin'
30      binstatus = status.metadata[grpc_status_details_bin_trailer]
31      return nil if binstatus.nil?
32
33      # Lazily load grpc_c and protobuf_c.so for users of this method.
34      require_relative './grpc'
35      require 'google/rpc/status_pb'
36
37      Google::Rpc::Status.decode(binstatus)
38    end
39  end
40end
41