error_examples_client.rb 2.6 KB

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