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

Add Status parameter to GrpcExceptionHandlerFunction.apply() method. #5786

Merged
merged 4 commits into from
Jun 27, 2024

Conversation

minwoox
Copy link
Contributor

@minwoox minwoox commented Jun 26, 2024

Motivation:
When an exception is raised, GrpcService creates a new Status from Status.getCause() via GrpcExceptionHandlerFunction. This can result in sending an incorrect status in certain situations:

Modifications:

  • Added a Status parameter to the GrpcExceptionHandlerFunction.apply() method.
  • Updated the default implementation of GrpcExceptionHandlerFunction.of() to return the provided Status if it is not an unknown status.

Result:

  • The GrpcExceptionHandlerFunction now properly handles and returns the correct Status.
  • (Breaking) The apply method of GrpcExceptionHandlerFunction now takes a Status.

Motivation:
When an exception is raised, `GrpcService` creates a new `Status` from `Status.getCause()` via `GrpcExceptionHandlerFunction`.
This can result in sending an incorrect status in certain situations:
- If the original exception is a `StatusRuntimeException` containing the `Status` that the user wants to send.
- The original exception has already been converted into a `Status` via `Status.fromThrowable()`: https://github.com/grpc/grpc-java/blob/5770114d08dcd352f2288ef52d17e1833530323c/stub/src/main/java/io/grpc/stub/ServerCalls.java#L389
- This converted `Status` is ignored and a new, potentially incorrect `Status` is created by `GrpcExceptionHandlerFunction` and sent.

Modifications:
- Added a `Status` parameter to the `GrpcExceptionHandlerFunction.apply()` method.
- Updated the default implementation of `GrpcExceptionHandlerFunction.of()` to return the provided `Status` if it is not an unknown status.

Result:
- The `GrpcExceptionHandlerFunction` now properly handles and returns the correct `Status`.
- (Breaking) The `apply` method of `GrpcExceptionHandlerFunction` now takes a `Status`.
@minwoox minwoox added this to the 1.29.1 milestone Jun 26, 2024
Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me once @trustin 's comments are addressed 👍 👍 👍

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @minwoox!

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a nit for @Nullable.

@@ -11,7 +11,7 @@ class GrpcExceptionHandler implements GrpcExceptionHandlerFunction {

@Nullable
@Override
public Status apply(RequestContext ctx, Throwable cause, Metadata metadata) {
public Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
public Status apply(RequestContext ctx, @Nullable Status status, Throwable cause, Metadata metadata) {

@@ -40,15 +40,15 @@ public interface GoogleGrpcExceptionHandlerFunction extends GrpcExceptionHandler

@Nullable
@Override
default Status apply(RequestContext ctx, Throwable throwable, Metadata metadata) {
default Status apply(RequestContext ctx, Status status, Throwable throwable, Metadata metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default Status apply(RequestContext ctx, Status status, Throwable throwable, Metadata metadata) {
default Status apply(RequestContext ctx, @Nullable Status status, Throwable throwable, Metadata metadata) {

@@ -35,9 +35,9 @@ public UnwrappingGrpcExceptionHandleFunction(GrpcExceptionHandlerFunction handle
}

@Override
public @Nullable Status apply(RequestContext ctx, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, @Nullable Status status, Throwable cause, Metadata metadata) {

@@ -145,7 +145,7 @@ private static class FirstGrpcExceptionHandler implements GrpcExceptionHandlerFu
private static class SecondGrpcExceptionHandler implements GrpcExceptionHandlerFunction {

@Override
public @Nullable Status apply(RequestContext ctx, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, @Nullable Status status, Throwable cause, Metadata metadata) {

@@ -133,7 +133,7 @@ void exceptionHandler() {
private static class FirstGrpcExceptionHandler implements GrpcExceptionHandlerFunction {

@Override
public @Nullable Status apply(RequestContext ctx, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public @Nullable Status apply(RequestContext ctx, Status status, Throwable cause, Metadata metadata) {
public @Nullable Status apply(RequestContext ctx, @Nullable Status status, Throwable cause, Metadata metadata) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All addressed. Thanks! 😉

@minwoox minwoox merged commit adfb913 into line:main Jun 27, 2024
13 of 15 checks passed
@minwoox minwoox deleted the add_status_grpc_exception_handler branch June 27, 2024 12:21
minwoox added a commit that referenced this pull request Jun 28, 2024
…od. (#5786)

Motivation:
When an exception is raised, `GrpcService` creates a new `Status` from `Status.getCause()` via `GrpcExceptionHandlerFunction`. This can result in sending an incorrect status in certain situations:
- If the original exception is a `StatusRuntimeException` containing the `Status` that the user wants to send.
- The original exception has already been converted into a `Status` via `Status.fromThrowable()`: https://github.com/grpc/grpc-java/blob/5770114d08dcd352f2288ef52d17e1833530323c/stub/src/main/java/io/grpc/stub/ServerCalls.java#L389
- This converted `Status` is ignored and a new, potentially incorrect `Status` is created by `GrpcExceptionHandlerFunction` and sent.

Modifications:
- Added a `Status` parameter to the `GrpcExceptionHandlerFunction.apply()` method.
- Updated the default implementation of `GrpcExceptionHandlerFunction.of()` to return the provided `Status` if it is not an unknown status.

Result:
- The `GrpcExceptionHandlerFunction` now properly handles and returns the correct `Status`.
- (Breaking) The `apply` method of `GrpcExceptionHandlerFunction` now takes a `Status`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants