Book Image

Practical gRPC

By : Joshua B. Humphries, David Konsumer, David Muto, Robert Ross, Carles Sistare
Book Image

Practical gRPC

By: Joshua B. Humphries, David Konsumer, David Muto, Robert Ross, Carles Sistare

Overview of this book

While building systems that contain several components, you need a framework that is fast and has minimal network overhead. gRPC is one such open-source tool that is quickly becoming popular and gaining popularity with programmers. Practical gRPC introduces you to gRPC and explains how it compares and contrasts with similar technologies. You’ll be introduced to key technologies such as Protocol Buffers, and work your way up from basic gRPC usage, all the way through to its more advanced capabilities. You’ll learn the best practices for defining and evolving your gRPC APIs, and discover how different tools can be leveraged to get the most out of gRPC and even extend it. By the end of this book, you'll have all the information you need to get started building systems with gRPC.
Table of Contents (13 chapters)
11
11. Extending gRPC services

Errors

The above examples show how to handle a successful RPC when the client successfully receives a response from the server. But what happens if our request is invalid? Or if there is a bug in the server that results in an error?

Earlier in the chapter, when we were creating the Go server, we introduced the gRPC error codes: sixteen error codes that servers can use when categorizing an error. The Go server has a simple example: if the client requests an unknown film ID, it sends back a Not Found error code.

In RPC clients, such an error will manifest as an exception:

Ruby

# Our server is hard-coded to know about IDs 4-6. It does
# not know about ID 7.
request = GetFilmRequest.new(id: '7')
begin
  response = stub.get_film(request)
  puts response.inspect
rescue GRPC::BadStatus => e
  # RPC exceptions have a numeric 'code' field (the gRPC error code)
  # and a string 'details' (the message supplied by the server)
  puts "failed: #{e.details} (code...