xref: /aosp_15_r20/external/grpc-grpc/examples/ruby/errors_and_cancellation/error_examples_server.rb (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/usr/bin/env ruby
2# -*- coding: utf-8 -*-
3
4# Copyright 2015 gRPC authors.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18# Error-throwing implementation of Route Guide service.
19#
20# Usage: $ path/to/route_guide_server.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
31include GRPC::Core::StatusCodes
32
33# CanellingandErrorReturningServiceImpl provides an implementation of the RouteGuide service.
34class CancellingAndErrorReturningServerImpl < RouteGuide::Service
35  # def get_feature
36  #   Note get_feature isn't implemented in this subclass, so the server
37  #   will get a gRPC UNIMPLEMENTED error when it's called.
38
39  def list_features(rectangle, _call)
40    raise "string appears on the client in the 'details' field of a 'GRPC::Unknown' exception"
41  end
42
43  def record_route(call)
44    raise GRPC::BadStatus.new_status_exception(CANCELLED)
45  end
46
47  def route_chat(notes)
48    raise GRPC::BadStatus.new_status_exception(ABORTED, details = 'arbitrary', metadata = {somekey: 'val'})
49  end
50end
51
52def main
53  port = '0.0.0.0:50051'
54  s = GRPC::RpcServer.new
55  s.add_http2_port(port, :this_port_is_insecure)
56  GRPC.logger.info("... running insecurely on #{port}")
57  s.handle(CancellingAndErrorReturningServerImpl.new)
58  s.run_till_terminated
59end
60
61main
62