forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor GrpcStatus to implement GrpcExceptionHandlerFunction (line#5571
) Motivation: - Closes line#5550 Modifications: - `GrpcStatus` implements `GrpcExceptionHandlerFunction` - Rename `GrpcStatus` to `DefaultGrpcExceptionHandlerFunction` Result: - Closes line#5550 - GrpcClientBuilder uses `DefaultGrpcExceptionHandlerFunction` as default - GrpcService can use `DefaultGrpcExceptionHandlerFunction` for its `exceptionHandler` <!-- Visit this URL to learn more about how to write a pull request description: https://armeria.dev/community/developer-guide#how-to-write-pull-request-description -->
- Loading branch information
1 parent
55d5eef
commit 505ae9a
Showing
21 changed files
with
341 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
grpc/src/main/java/com/linecorp/armeria/common/grpc/DefaultGrpcExceptionHandlerFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.grpc; | ||
|
||
import java.io.IOException; | ||
import java.nio.channels.ClosedChannelException; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
|
||
import com.linecorp.armeria.client.UnprocessedRequestException; | ||
import com.linecorp.armeria.client.circuitbreaker.FailFastException; | ||
import com.linecorp.armeria.common.ClosedSessionException; | ||
import com.linecorp.armeria.common.ContentTooLargeException; | ||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.TimeoutException; | ||
import com.linecorp.armeria.common.stream.ClosedStreamException; | ||
import com.linecorp.armeria.server.RequestTimeoutException; | ||
|
||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import io.grpc.Status.Code; | ||
import io.netty.handler.codec.http2.Http2Error; | ||
import io.netty.handler.codec.http2.Http2Exception; | ||
|
||
enum DefaultGrpcExceptionHandlerFunction implements GrpcExceptionHandlerFunction { | ||
INSTANCE; | ||
|
||
/** | ||
* Converts the {@link Throwable} to a {@link Status}, taking into account exceptions specific to Armeria as | ||
* well and the protocol package. | ||
*/ | ||
@Override | ||
public Status apply(RequestContext ctx, Throwable cause, Metadata metadata) { | ||
final Status s = Status.fromThrowable(cause); | ||
if (s.getCode() != Code.UNKNOWN) { | ||
return s; | ||
} | ||
|
||
if (cause instanceof ClosedSessionException || cause instanceof ClosedChannelException) { | ||
// ClosedChannelException is used any time the Netty channel is closed. Proper error | ||
// processing requires remembering the error that occurred before this one and using it | ||
// instead. | ||
return s; | ||
} | ||
if (cause instanceof ClosedStreamException || cause instanceof RequestTimeoutException) { | ||
return Status.CANCELLED.withCause(cause); | ||
} | ||
if (cause instanceof InvalidProtocolBufferException) { | ||
return Status.INVALID_ARGUMENT.withCause(cause); | ||
} | ||
if (cause instanceof UnprocessedRequestException || | ||
cause instanceof IOException || | ||
cause instanceof FailFastException) { | ||
return Status.UNAVAILABLE.withCause(cause); | ||
} | ||
if (cause instanceof Http2Exception) { | ||
if (cause instanceof Http2Exception.StreamException && | ||
((Http2Exception.StreamException) cause).error() == Http2Error.CANCEL) { | ||
return Status.CANCELLED; | ||
} | ||
return Status.INTERNAL.withCause(cause); | ||
} | ||
if (cause instanceof TimeoutException) { | ||
return Status.DEADLINE_EXCEEDED.withCause(cause); | ||
} | ||
if (cause instanceof ContentTooLargeException) { | ||
return Status.RESOURCE_EXHAUSTED.withCause(cause); | ||
} | ||
return s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.