There are three microservices that communicate with aggregating microservice by grpc. Each of microservices can return an error (4xx-5xx), the question is how to throw this exception from microservice on grpc into aggregating microservice?
Example:
in one of microservices there is a method that throws an exception:
@Override public void getAllMovies(gRPCGetAllMoviesRequest request, StreamObserver<gRPCGetAllMoviesResponse> responseObserver) { try { Page<MovieEntity> allMovies = movieService.getAllMovies(ProtoConvertToEntity.convert(request.getPageable())); responseObserver.onNext(gRPCGetAllMoviesResponse.newBuilder(). addAllMovies(allMovies .stream() .map(ProtoConvertToEntity::convert) .collect(Collectors.toList())) .build()); responseObserver.onCompleted(); } catch (Exception e) { ErrorStatus.status(responseObserver, e); } } Method that returns an error to the aggregation microservice:
public class ErrorStatus { public static void status(StreamObserver<?> responseObserver, Exception e) { Status status; if (e instanceof IllegalArgumentException) { status = Status.INVALID_ARGUMENT; } else if (e instanceof NotFoundMovieException) { status = Status.NOT_FOUND; } else { status = Status.INTERNAL; } } } and how to take on the side of the aggregating microservice exception and handle it, I do not know.