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

Streamline the creation of GRPC Exceptions #2571

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ public static GrpcStatus fromCodeValue(int codeValue) {
new GrpcStatus(UNKNOWN, "Unknown code: " + codeValue) : INT_TO_GRPC_STATUS_MAP[codeValue];
}

/**
* Obtains the status given an integer code value and a description String.
* @param codeValue integer code value.
* @param description description for the given status.
* @return status associated with the code value, or {@link GrpcStatusCode#UNKNOWN}.
*/
public static GrpcStatus fromCodeValueAndDescription(int codeValue, String description) {
if (codeValue < 0 || codeValue >= INT_TO_GRPC_STATUS_MAP.length) {
return new GrpcStatus(UNKNOWN, "Unknown code: " + codeValue);
} else {
return new GrpcStatus(GrpcStatusCode.fromCodeValue(codeValue), description);
}
}

/**
* Translates a throwable into a status.
*
Expand Down Expand Up @@ -156,10 +170,8 @@ public static GrpcStatus fromThrowableNullable(Throwable t) { // FIXME: 0.43 - r
* Returns the current status wrapped in a {@link GrpcStatusException}.
*
* @return the current status wrapped in a {@link GrpcStatusException}.
* @deprecated Use {@link GrpcStatusException#GrpcStatusException(GrpcStatus)}.
*/
@Deprecated
public GrpcStatusException asException() { // FIXME: 0.43 - remove deprecated method
public GrpcStatusException asException() {
Copy link

@82baras 82baras Jul 22, 2023

Choose a reason for hiding this comment

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

Should we update deprecated code?

return new GrpcStatusException(this, () -> null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,14 @@ public static GrpcStatusCode fromHttp2ErrorCode(Http2ErrorCode errorCode) {
return h2ErrorCode < 0 || h2ErrorCode >= H2_ERROR_TO_STATUS_CODE_MAP.length ?
UNKNOWN : H2_ERROR_TO_STATUS_CODE_MAP[h2ErrorCode];
}

/**
* Returns a standard {@link GrpcStatus} with description as part of a {@link GrpcStatusException} builder pattern.
* @param description the Description for pending {@link GrpcStatusException}.
* @return the GrpcStatus with set description
*/
public GrpcStatus withDescription(String description) {
GrpcStatus grpcStatus = GrpcStatus.fromCodeValueAndDescription(this.value(), description);
return grpcStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class GrpcStatusTest {

Expand Down Expand Up @@ -56,4 +58,16 @@ void testGrpcStatusUnknownFromUnparsableStringCodeValue() {
assertEquals(GrpcStatusCode.UNKNOWN, grpcStatus.code());
assertEquals("Status code value not a number: " + unknownCode, grpcStatus.description());
}

@Test
void testGrpcStatusExceptionFromGrpcStatusCode() {
final String exceptionMessage = "denied!";
GrpcStatusException grpcStatusException = GrpcStatusCode.PERMISSION_DENIED.withDescription(exceptionMessage).asException();
Exception thrownGrpcStatusException = assertThrows(GrpcStatusException.class, () -> {
throw grpcStatusException;
});
final String expectedExceptionMessage = exceptionMessage;
final String actualMessage = thrownGrpcStatusException.getMessage();
assertTrue(actualMessage.contains(expectedExceptionMessage));
}
}