简体   繁体   中英

Handling gRPC server runtime exceptions in a proper way

I try to find out how the exception handling mechanism in gRPC works.

Are there any other ways except try-catch block to handle runtime exceptions such as IllegalArgumentException on server side?

For example, I have some gRPC streaming-client service, where the method onNext throws IllegalArgumentException in cases when passed parameters are not satisfied some asserts on the deep level (in some library, eg com.google.common.base.Preconditions ).

In this case there will be StatusRuntimeException with Status.UNKNOWN on the client side but is there another solution that allows not to handle such exceptions and convert them after service call to readable StatusRuntimeException ?

Normally you should catch all exceptions on server side, and convert them into the proper StatusRuntimeException or StatusException . However, you can avoid this by using the TransmitStatusRuntimeExceptionInterceptor class with your server to automatically convert the error for you.

The reason it isn't typically used is because it means that unexpected errors appear the same as expected ones. You may not want to propagate an exception message, perhaps because it has private user data, and instead rewrite it in your Server handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM