error_examples_server.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env ruby
  2. # -*- coding: utf-8 -*-
  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. # Error-throwing implementation of Route Guide service.
  17. #
  18. # Usage: $ path/to/route_guide_server.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. include GRPC::Core::StatusCodes
  26. # CanellingandErrorReturningServiceImpl provides an implementation of the RouteGuide service.
  27. class CancellingAndErrorReturningServerImpl < RouteGuide::Service
  28. # def get_feature
  29. # Note get_feature isn't implemented in this subclass, so the server
  30. # will get a gRPC UNIMPLEMENTED error when it's called.
  31. def list_features(rectangle, _call)
  32. raise "string appears on the client in the 'details' field of a 'GRPC::Unknown' exception"
  33. end
  34. def record_route(call)
  35. raise GRPC::BadStatus.new_status_exception(CANCELLED)
  36. end
  37. def route_chat(notes)
  38. raise GRPC::BadStatus.new_status_exception(ABORTED, details = 'arbitrary', metadata = {somekey: 'val'})
  39. end
  40. end
  41. def main
  42. port = '0.0.0.0:50051'
  43. s = GRPC::RpcServer.new
  44. s.add_http2_port(port, :this_port_is_insecure)
  45. GRPC.logger.info("... running insecurely on #{port}")
  46. s.handle(CancellingAndErrorReturningServerImpl.new)
  47. s.run_till_terminated
  48. end
  49. main