Reputation: 99
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, e.g. 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
?
Upvotes: 2
Views: 5790
Reputation: 6628
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.
Upvotes: 4