Spring Boot + gRPC Error Handling Example
Error handling in gRPC client and server involves managing and reacting to errors that may occur during communication between the client and server. In gRPC, there are different types of errors that can occur, such as network errors, server errors, security errors, and protocol errors.
In gRPC, error handling is typically implemented using gRPC status codes and status messages. When an error occurs, the server will send an appropriate status code and message to the client, which can then handle the error accordingly.
In the client, error handling can involve checking the status code of the response and taking appropriate action, such as retrying the request, logging the error, or displaying an error message to the user.
gRPC provides users with well defined status codes as part of the RPC API that can be made use of during error handling.
Code | Number |
---|---|
OK | 0 |
CANCELLED | 1 |
UNKNOWN | 2 |
INVALID_ARGUMENT | 3 |
DEADLINE_EXCEEDED | 4 |
NOT_FOUND | 5 |
ALREADY_EXISTS | 6 |
PERMISSION_DENIED | 7 |
RESOURCE_EXHAUSTED | 8 |
FAILED_PRECONDITION | 9 |
ABORTED | 10 |
OUT_OF_RANGE | 11 |
UNIMPLEMENTED | 12 |
INTERNAL | 13 |
UNAVAILABLE | 14 |
DATA_LOSS | 15 |
UNAUTHENTICATED | 16 |
Video
This tutorial is explained in the below Youtube Video.gRPC - Table of Contents
Spring Boot+ gRPC Hello World Example Spring Boot gRPC Server + C# gRPC Client Example Spring Boot 3 + gRPC - Types of gRPC Spring Boot 3 + gRPC Unary Example Spring Boot 3 + gRPC Server Streaming Example Spring Boot 3 + gRPC Client Streaming Example Spring Boot 3 + gRPC Bidirectional Streaming Example Spring Boot + gRPC Deadline Example Spring Boot + gRPC Error Handling Example Spring Boot + gRPC Error Handling - Using Trailer Metadata Spring Boot + gRPC Error Handling - Global Exception Handler Using GrpcAdvice
Implementation
We will be modifying the code we implemented in a previous tutorial - Spring Boot 3 + gRPC Unary Example. We will be implementing error handling code to return the gRPC status code and the description from the gRPC server to the client.
package com.javainuse.bank.service; import com.javainuse.banking.AccountBalanceResponse; import com.javainuse.banking.AccountBalanceServiceGrpc; import com.javainuse.banking.AccountRequest; import io.grpc.Status; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; @GrpcService public class BankAccountBalanceService extends AccountBalanceServiceGrpc.AccountBalanceServiceImplBase { @Override public void getAccountBalance(AccountRequest request, StreamObserver<com.javainuse.banking.AccountBalanceResponse> responseObserver) { if ((request.getAccountNumber().equals("account5"))) { responseObserver.onError((Status.NOT_FOUND.withDescription("The requested Account Number cannot be found.")) .asRuntimeException()); return; } AccountBalanceResponse empResp = AccountBalanceResponse.newBuilder() .setAccountNumber(request.getAccountNumber()).setBalance(100).build(); // set the response object responseObserver.onNext(empResp); // mark process is completed responseObserver.onCompleted(); } }Start the gRPC server.