From 168f53a90cc109e8b1dec4f6a1d3e0ac711da140 Mon Sep 17 00:00:00 2001 From: emeroad Date: Mon, 17 Jun 2024 18:44:14 +0900 Subject: [PATCH] [#noissue] Enhanced exception handling --- .../navercorp/pinpoint/grpc/StatusErrors.java | 56 ++++++++++++++----- .../pinpoint/grpc/StatusErrorsTest.java | 28 ++++++++++ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java b/grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java index 97daa7e47381..e7a9e495d506 100644 --- a/grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java +++ b/grpc/src/main/java/com/navercorp/pinpoint/grpc/StatusErrors.java @@ -18,6 +18,7 @@ import com.navercorp.pinpoint.common.util.StringUtils; import io.grpc.Status; +import io.grpc.StatusException; import io.grpc.StatusRuntimeException; /** @@ -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) { diff --git a/grpc/src/test/java/com/navercorp/pinpoint/grpc/StatusErrorsTest.java b/grpc/src/test/java/com/navercorp/pinpoint/grpc/StatusErrorsTest.java index af42366553e2..9a4e637cd7fb 100644 --- a/grpc/src/test/java/com/navercorp/pinpoint/grpc/StatusErrorsTest.java +++ b/grpc/src/test/java/com/navercorp/pinpoint/grpc/StatusErrorsTest.java @@ -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; @@ -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()); + } } \ No newline at end of file