Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot throw custome runtime exception inside clientinterceptor #3434

Closed
rezadhah opened this issue Sep 4, 2017 · 4 comments
Closed

Cannot throw custome runtime exception inside clientinterceptor #3434

rezadhah opened this issue Sep 4, 2017 · 4 comments
Labels

Comments

@rezadhah
Copy link

rezadhah commented Sep 4, 2017

Please answer these questions before submitting your issue.

What version of gRPC are you using?

1.4.0

What JVM are you using (java -version)?

1.8.0_144

What did you do?

If possible, provide a recipe for reproducing the error.

So i was trying to use ClientInterceptor by overiding onMessage(RespT message). So i'm trying to throw my custome runtime exception inside ForwaringClientCall.listener something like this

Field code;

@Override
public void onMessage(RespT message) {
    try {
        getCode = message.getClass().getMethod("getCode");
        getMessage = message.getClass().getMethod("getMessage");
        returnCode = (int) getCode.invoke(message);
        returnMessage = (String) getMessage.invoke(message);
     } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {}
     
     if (returnCode != 200) {
          throw new CustomeRuntimeException(returnCode, returnMessage); // here
     }
     logger.info("{} fnished ...", methodName);
     super.onMessage(message);
}

What did you expect to see?

I excpect it will throw my custom exception

What did you see instead?

it always end inside inside this method catch (Throwable t) and it will end with statusruntimeexception cancel

@Override
public void messageRead(final InputStream message) {
  class MessageRead extends ContextRunnable {
    MessageRead() {
    super(context);
  }

  @Override
  public final void runInContext() {
   try {
       if (closed) {
          return;
   }
   try {
         observer.onMessage(method.parseResponse(message));
      } finally {
        message.close();
      }
    } catch (Throwable t) {
       Status status =
       Status.CANCELLED.withCause(t).withDescription("Failed to read message.");
       stream.cancel(status);
       close(status, new Metadata());
     }
  }
}

Thank you for your attention

@rezadhah rezadhah changed the title Cannot get field from GrpcServer via reflection Cannot get field from GrpcServer response via reflection Sep 4, 2017
@rezadhah rezadhah changed the title Cannot get field from GrpcServer response via reflection Cannot get field from GrpcServer response via reflection [question] Sep 4, 2017
@rezadhah rezadhah changed the title Cannot get field from GrpcServer response via reflection [question] Cannot get field from GrpcServer response via reflection [:question] Sep 4, 2017
@rezadhah rezadhah changed the title Cannot get field from GrpcServer response via reflection [:question] Cannot get field from GrpcServer response via reflection [label:question] Sep 4, 2017
@rezadhah rezadhah changed the title Cannot get field from GrpcServer response via reflection [label:question] Cannot get field from GrpcServer response via reflection [Question] Sep 4, 2017
@rezadhah
Copy link
Author

rezadhah commented Sep 4, 2017

Is it possible to throw my custom exception inside ClientInterceptor?

@rezadhah rezadhah changed the title Cannot get field from GrpcServer response via reflection [Question] Cannot throw custome runtime exception inside clientinterceptor Sep 4, 2017
@carl-mastrangelo
Copy link
Contributor

You can throw whatever you want, but we will always wrap it in a StatusRuntimeException or StatusException.

It will be set as the cause in the the Status.

@ejona86
Copy link
Member

ejona86 commented Sep 6, 2017

It's generally best not to throw within an interceptor. Instead call cancel() directly. Throwing should be more when there is a bug (so any exceptions from the reflection would probably be fine, but returnCode seems better for graceful handling.

@rezadhah
Copy link
Author

rezadhah commented Sep 7, 2017

Yes, i am currently in client team. We want to handle runtime exception by converting code (with http code standard) that server send as response because stacktrace thread server cannot be send to client. We have standard to use code and message to check what kind of error we get from server. So i thought that it can be handle with:

  • via ClientInterceptor in onMessage method because that function will give the response and i can check it via reflection
  • via aop, but i cannot target a pointcut to static final class (we use grpc + spring boot)
  • dynamic proxy, and again it cannot be done because there is no interface on generated stub

@lock lock bot locked and limited conversation to collaborators Sep 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants