Skip to content

Commit

Permalink
Streamlined the creation of GRPC Exceptions by adding convenience met…
Browse files Browse the repository at this point in the history
…hods.

Motivation:

In reference to issue apple#2502, there should be a more convenient way to initialize GRPC Exceptions analogous to other GRPC implementations.

Modifications:

- Removed the deprecation from GrpcStatus.asException as it is required for this implementation of desired functionality.
- Added additional constructor for GrpcStatus to support initialization from codeValue and description.
- Added convenience method GrpcStatusCode.withDescription to support desired functionality.
- Added GrpcStatusTest.testGrpcStatusExceptionFromGrpcStatusCode for test-driven development and better test coverage.

Result:

GRPC Exceptions can now be initialized and thrown more conveniently.

What is the result of this change?
  • Loading branch information
cmwigard committed May 1, 2023
1 parent d16bf60 commit f9b5929
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
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 @@ -152,14 +166,13 @@ public static GrpcStatus fromThrowableNullable(Throwable t) { // FIXME: 0.43 - r
return exception == null ? null : exception.status();
}


/**
* 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() {
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 @@ -16,8 +16,11 @@
package io.servicetalk.grpc.api;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestClassOrder;

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 +59,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));
}
}

0 comments on commit f9b5929

Please sign in to comment.