-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[GRPC] Map to proper GRPC status codes and achieve exception handling parity with HTTP #18925
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
352aaa1
[GRPC] Add proper GRPC status code and error handling
karenyrx e96fac9
rename Http to Rest, group together some convertRestToGrpcStatus codes
karenyrx 6b93cb6
Merge branch 'main' into errorhandling
karenyrx 859e0fa
Merge branch 'main' into errorhandling
karenyrx 5ff1ad2
Merge branch 'main' into errorhandling
karenyrx a0430d6
Merge branch 'main' into errorhandling
karenyrx 5dcf5c7
make final
karenyrx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
...les/transport-grpc/src/main/java/org/opensearch/transport/grpc/util/GrpcErrorHandler.java
This file contains hidden or 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,116 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.transport.grpc.util; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParseException; | ||
| import com.fasterxml.jackson.core.exc.InputCoercionException; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.ExceptionsHelper; | ||
| import org.opensearch.OpenSearchException; | ||
| import org.opensearch.core.compress.NotCompressedException; | ||
| import org.opensearch.core.compress.NotXContentException; | ||
| import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| import io.grpc.Status; | ||
| import io.grpc.StatusRuntimeException; | ||
|
|
||
| /** | ||
| * Converts exceptions to a GRPC StatusRuntimeException. | ||
| */ | ||
| public class GrpcErrorHandler { | ||
| private static final Logger logger = LogManager.getLogger(GrpcErrorHandler.class); | ||
|
|
||
| private GrpcErrorHandler() { | ||
| // Utility class, no instances | ||
| } | ||
|
|
||
| /** | ||
| * Converts an exception to an appropriate GRPC StatusRuntimeException. | ||
| * Uses shared constants from {@link ExceptionsHelper.ErrorMessages} and {@link ExceptionsHelper#summaryMessage} | ||
| * for exact parity with HTTP error handling. | ||
| * | ||
| * @param e The exception to convert | ||
| * @return StatusRuntimeException with appropriate GRPC status and HTTP-identical error messages | ||
| */ | ||
| public static StatusRuntimeException convertToGrpcError(Exception e) { | ||
| // ========== OpenSearch Business Logic Exceptions ========== | ||
| // Custom OpenSearch exceptions which extend {@link OpenSearchException}. | ||
| // Uses {@link HttpToGrpcStatusConverter} for HTTP -> gRPC status mapping and | ||
| // follows {@link OpenSearchException#generateFailureXContent} unwrapping logic | ||
| if (e instanceof OpenSearchException) { | ||
| return handleOpenSearchException((OpenSearchException) e); | ||
| } | ||
|
|
||
| // ========== OpenSearch Core System Exceptions ========== | ||
| // Low-level OpenSearch exceptions that don't extend OpenSearchException - include full details | ||
| else if (e instanceof OpenSearchRejectedExecutionException) { | ||
| return Status.RESOURCE_EXHAUSTED.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof NotXContentException) { | ||
| return Status.INVALID_ARGUMENT.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof NotCompressedException) { | ||
| return Status.INVALID_ARGUMENT.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } | ||
|
|
||
| // ========== 3. Third-party Library Exceptions ========== | ||
| // External library exceptions (Jackson JSON parsing) - include full details | ||
| else if (e instanceof InputCoercionException) { | ||
| return Status.INVALID_ARGUMENT.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof JsonParseException) { | ||
| return Status.INVALID_ARGUMENT.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } | ||
|
|
||
| // ========== 4. Standard Java Exceptions ========== | ||
| // Generic Java runtime exceptions - include full exception details for debugging | ||
| else if (e instanceof IllegalArgumentException) { | ||
| return Status.INVALID_ARGUMENT.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof IllegalStateException) { | ||
| return Status.FAILED_PRECONDITION.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof SecurityException) { | ||
| return Status.PERMISSION_DENIED.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof TimeoutException) { | ||
| return Status.DEADLINE_EXCEEDED.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof InterruptedException) { | ||
| return Status.CANCELLED.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } else if (e instanceof IOException) { | ||
| return Status.INTERNAL.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } | ||
|
|
||
| // ========== 5. Unknown/Unmapped Exceptions ========== | ||
| // Safety fallback for any unexpected exception to {@code Status.INTERNAL} with full debugging info | ||
| else { | ||
| logger.warn("Unmapped exception type: {}, treating as INTERNAL error", e.getClass().getSimpleName()); | ||
| return Status.INTERNAL.withDescription(ExceptionsHelper.stackTrace(e)).asRuntimeException(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handles OpenSearch-specific exceptions by converting their HTTP status to GRPC status. | ||
| * Uses {@link ExceptionsHelper#summaryMessage(Throwable)} for exact parity with HTTP error handling. | ||
| * | ||
| * Uses {@link ExceptionsHelper#unwrapToOpenSearchException(Throwable)} for shared unwrapping logic | ||
| * with HTTP's {@link OpenSearchException#generateFailureXContent}. | ||
| * | ||
| * @param e The {@link OpenSearchException} to convert | ||
| * @return StatusRuntimeException with mapped GRPC status and HTTP-identical error message | ||
| */ | ||
| private static StatusRuntimeException handleOpenSearchException(OpenSearchException e) { | ||
| Status grpcStatus = HttpToGrpcStatusConverter.convertHttpToGrpcStatus(e.status()); | ||
|
|
||
| Throwable unwrapped = ExceptionsHelper.unwrapToOpenSearchException(e); | ||
|
|
||
| String description = ExceptionsHelper.summaryMessage(unwrapped); | ||
| return grpcStatus.withDescription(description).asRuntimeException(); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.