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

Can not inherit and extend classes annotated with @GrpcAdvice and containing @GrpcExceptionHandlers #788

Open
Dhi13man opened this issue Nov 22, 2022 · 1 comment
Labels
bug Something does not work as expected
Milestone

Comments

@Dhi13man
Copy link

The Problem

Spring allows classes annotated with @ControllerAdvice and containing @ExceptionHandlers to be extended and overridden as necessary, such that the behaviours of the overridden methods take precedence over base methods.

It seems we can not similarly inherit and extend classes annotated with @GrpcAdvice and containing @GrpcExceptionHandlers.

Steps to Reproduce

With Proper Annotations and Overriding in Derived Class

If one tries to extend one @GrpcAdvice class with another, providing the @GrpcExceptionHandler annotations as shown below, the application is unable to start up with Fatal Exception: Ambiguous "@GrpcExceptionHandler" method mapped for BadRequestException..

@GrpcAdvice
public class BaseGrpcExceptionHandler {

  @GrpcExceptionHandler(BadRequestException.class)
  public Status handleBadRequestException(BadRequestException ex) {
    LOGGER.error("Bad Request Sent to Service: ", ex).log();
    return embedStatusWithExceptionData(Status.INVALID_ARGUMENT, ex);
  }
}

@GrpcAdvice
public class SpecialisedGrpcExceptionHandler extends BaseGrpcExceptionHandler {

  @Override
  @GrpcExceptionHandler(BadRequestException.class)
  public Status handleBadRequestException(BadRequestException ex) {
    final Status internalErrorStatus = super.handleBadRequestException(ex);
    return resolveExceptionResponseDtoForClientSide(internalErrorStatus);
  }

  private Status resolveExceptionResponseDtoForClientSide(Status status) {
    // ...Some custom logic
  }
}

Without @GrpcExceptionHandler annotation in Derived Class

If one tries to extend one @GrpcAdvice class with another as shown below, without @GrpcExceptionHandler annotation, the application is unable to start up with Fatal Exception: [net/devh/boot/grpc/server/autoconfigure/GrpcAdviceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: @GrpcExceptionHandler annotation not found..

@GrpcAdvice
public class BaseGrpcExceptionHandler {

  @GrpcExceptionHandler(BadRequestException.class)
  public Status handleBadRequestException(BadRequestException ex) {
    LOGGER.error("Bad Request Sent to Service: ", ex).log();
    return embedStatusWithExceptionData(Status.INVALID_ARGUMENT, ex);
  }
}

@GrpcAdvice
public class SpecialisedGrpcExceptionHandler extends BaseGrpcExceptionHandler {

  @Override
  public Status handleBadRequestException(BadRequestException ex) {
    final Status internalErrorStatus = super.handleBadRequestException(ex);
    return resolveExceptionResponseDtoForClientSide(internalErrorStatus);
  }

  private Status resolveExceptionResponseDtoForClientSide(Status status) {
    // ...Some custom logic
  }
}

Additional Context

The dependency being used is:

    <dependency>
      <artifactId>grpc-server-spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <artifactId>grpc-core</artifactId>
          <groupId>io.grpc</groupId>
        </exclusion>
      </exclusions>
      <groupId>net.devh</groupId>
      <version>2.13.1.RELEASE</version>
    </dependency>

Other versions being used are as follows:

<properties>
    <grpc.version>1.46.0</grpc.version>
    <java.version>11</java.version>
    <project.version>0.0.1-SNAPSHOT</project.version>
    <protobuf-plugin.version>0.6.1</protobuf-plugin.version>
    <protobuf.version>3.20.1</protobuf.version>
    <spring.boot.version>2.5.8</spring.boot.version>
    <spring.cloud.kubernetes.version>1.1.10.RELEASE</spring.cloud.kubernetes.version>
    <spring.cloud.version>2020.0.5</spring.cloud.version>
  </properties>
@Dhi13man Dhi13man added the bug Something does not work as expected label Nov 22, 2022
@ST-DDT
Copy link
Collaborator

ST-DDT commented Nov 27, 2022

I'll have a look/try to fix that soon.

@ST-DDT ST-DDT added this to the 2.15.0 milestone Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as expected
Projects
None yet
Development

No branches or pull requests

2 participants