xref: /aosp_15_r20/external/grpc-grpc/examples/ruby/errors_and_cancellation/error_examples_client.rb (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env ruby
2
3# Copyright 2015 gRPC authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Sample app that connects to an error-throwing implementation of
18# Route Guide service.
19#
20# Usage: $ path/to/route_guide_client.rb
21
22this_dir = File.expand_path(File.dirname(__FILE__))
23lib_dir = File.join(File.dirname(this_dir), 'lib')
24$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
25
26require 'grpc'
27require 'route_guide_services_pb'
28
29include Routeguide
30
31def run_get_feature_expect_error(stub)
32  resp = stub.get_feature(Point.new)
33end
34
35def run_list_features_expect_error(stub)
36  resps = stub.list_features(Rectangle.new)
37
38  # NOOP iteration to pick up error
39  resps.each { }
40end
41
42def run_record_route_expect_error(stub)
43  stub.record_route([])
44end
45
46def run_route_chat_expect_error(stub)
47  resps = stub.route_chat([])
48
49  # NOOP iteration to pick up error
50  resps.each { }
51end
52
53def main
54  stub = RouteGuide::Stub.new('localhost:50051', :this_channel_is_insecure)
55
56  begin
57    run_get_feature_expect_error(stub)
58  rescue GRPC::BadStatus => e
59    puts "===== GetFeature exception: ====="
60    puts e.inspect
61    puts "e.code: #{e.code}"
62    puts "e.details: #{e.details}"
63    puts "e.metadata: #{e.metadata}"
64    puts "================================="
65  end
66
67  begin
68    run_list_features_expect_error(stub)
69  rescue GRPC::BadStatus => e
70    error = true
71    puts "===== ListFeatures exception: ====="
72    puts e.inspect
73    puts "e.code: #{e.code}"
74    puts "e.details: #{e.details}"
75    puts "e.metadata: #{e.metadata}"
76    puts "================================="
77  end
78
79  begin
80    run_route_chat_expect_error(stub)
81  rescue GRPC::BadStatus => e
82    puts "==== RouteChat exception: ===="
83    puts e.inspect
84    puts "e.code: #{e.code}"
85    puts "e.details: #{e.details}"
86    puts "e.metadata: #{e.metadata}"
87    puts "================================="
88  end
89
90  begin
91    run_record_route_expect_error(stub)
92  rescue GRPC::BadStatus => e
93    puts "==== RecordRoute exception: ===="
94    puts e.inspect
95    puts "e.code: #{e.code}"
96    puts "e.details: #{e.details}"
97    puts "e.metadata: #{e.metadata}"
98    puts "================================="
99  end
100end
101
102main
103