Skip to content

Commit

Permalink
[#noissue] Enhanced exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jun 17, 2024
1 parent db62e19 commit 168f53a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
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 @@ public class StatusErrors {
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));
}
}
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());
}
}

0 comments on commit 168f53a

Please sign in to comment.