Skip to content

[#noissue] Enhanced exception handling #11157

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 1 commit into from
Jun 17, 2024
Merged
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
56 changes: 41 additions & 15 deletions grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.navercorp.pinpoint.common.util.StringUtils;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;

/**
Expand All @@ -28,24 +29,49 @@
static final String CANCELLED_BEFORE_RECEIVING_HALF_CLOSE = "CANCELLED: cancelled before receiving half close";


public static StatusError throwable(final Throwable t) {
if (t instanceof StatusRuntimeException) {
final StatusRuntimeException exception = (StatusRuntimeException) t;
final Status.Code code = exception.getStatus().getCode();
if (code == Status.UNAVAILABLE.getCode()) {
final String causeMessage = findCauseMessage(t, CONNECTION_REFUSED_MESSAGE, 2);
if (causeMessage != null) {
final String message = exception.getStatus().getDescription() + ": " + causeMessage;
return new SimpleStatusError(message, t);
}
} else if (code == Status.CANCELLED.getCode()) {
final String message = exception.getMessage();
if (StringUtils.contains(message, CANCELLED_BEFORE_RECEIVING_HALF_CLOSE)) {
return new SimpleStatusError(CANCELLED_BEFORE_RECEIVING_HALF_CLOSE, t);
// @NonNull
public static StatusError throwable(final Throwable throwable) {
final Status status = getStatus(throwable);
if (status != null) {
try {
final SimpleStatusError message = getSimpleStatusError(status, throwable);
if (message != null) {
return message;
}
} catch (Throwable parseError) {
return new DefaultStatusError(new RuntimeException("Status parse error", parseError));

Check warning on line 42 in grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java

View check run for this annotation

Codecov / codecov/patch

grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java#L41-L42

Added lines #L41 - L42 were not covered by tests
}
}
return new DefaultStatusError(throwable);
}

private static Status getStatus(Throwable throwable) {
if (throwable instanceof StatusRuntimeException) {
final StatusRuntimeException exception = (StatusRuntimeException) throwable;
return exception.getStatus();
}
if (throwable instanceof StatusException) {
final StatusException exception = (StatusException) throwable;
return exception.getStatus();
}
return null;
}

private static SimpleStatusError getSimpleStatusError(Status status, Throwable throwable) {
final Status.Code code = status.getCode();
if (code == Status.UNAVAILABLE.getCode()) {
final String causeMessage = findCauseMessage(throwable, CONNECTION_REFUSED_MESSAGE, 2);
if (causeMessage != null) {
final String message = status.getDescription() + ": " + causeMessage;
return new SimpleStatusError(message, throwable);
}
} else if (code == Status.CANCELLED.getCode()) {
final String message = throwable.getMessage();
if (StringUtils.contains(message, CANCELLED_BEFORE_RECEIVING_HALF_CLOSE)) {
return new SimpleStatusError(CANCELLED_BEFORE_RECEIVING_HALF_CLOSE, throwable);
}
}
return new DefaultStatusError(t);
return null;
}

private static String findCauseMessage(final Throwable t, final String message, final int maxDepth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.grpc;

import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -91,4 +92,31 @@ public void throwable_status_cancel() {
.contains(Status.CANCELLED.getCode().toString());
assertTrue(statusError.isSimpleError());
}


@Test
public void test_asException() {
StatusError statusError = StatusErrors.throwable(Status.CANCELLED.asException());
assertEquals("CANCELLED", statusError.getMessage());
assertFalse(statusError.isSimpleError());
}


@Test
public void simpleCode_null() {

StatusException exception = Status.CANCELLED.withCause(null).withDescription(null).asException();
StatusError statusError = StatusErrors.throwable(exception);
assertEquals("CANCELLED", statusError.getMessage());
assertFalse(statusError.isSimpleError());
}

@Test
public void nonSimpleCode_null() {

StatusException exception = Status.ABORTED.withCause(null).withDescription(null).asException();
StatusError statusError = StatusErrors.throwable(exception);
assertEquals("ABORTED", statusError.getMessage());
assertFalse(statusError.isSimpleError());
}
}