diff --git a/changelog/@unreleased/pr-604.v2.yml b/changelog/@unreleased/pr-604.v2.yml new file mode 100644 index 000000000..813f6d1f0 --- /dev/null +++ b/changelog/@unreleased/pr-604.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: move `RemoteException`s errorInstanceId from exception message into + exception args + links: + - https://github.com/palantir/conjure-java-runtime-api/pull/604 diff --git a/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java b/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java index 775eee59d..205be7334 100644 --- a/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java +++ b/errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java @@ -17,6 +17,7 @@ package com.palantir.conjure.java.api.errors; import com.palantir.logsafe.Arg; +import com.palantir.logsafe.SafeArg; import com.palantir.logsafe.SafeLoggable; import java.util.Collections; import java.util.List; @@ -25,8 +26,11 @@ public final class RemoteException extends RuntimeException implements SafeLoggable { private static final long serialVersionUID = 1L; + private final String message; + private final String stableMessage; private final SerializableError error; private final int status; + private final List> args; /** Returns the error thrown by a remote process which caused an RPC call to fail. */ public SerializableError getError() { @@ -39,27 +43,29 @@ public int getStatus() { } public RemoteException(SerializableError error, int status) { - super( - error.errorCode().equals(error.errorName()) - ? String.format( - "RemoteException: %s with instance ID %s", error.errorCode(), error.errorInstanceId()) - : String.format( - "RemoteException: %s (%s) with instance ID %s", - error.errorCode(), error.errorName(), error.errorInstanceId())); - + this.stableMessage = error.errorCode().equals(error.errorName()) + ? String.format("RemoteException: %s", error.errorCode()) + : String.format("RemoteException: %s (%s)", error.errorCode(), error.errorName()); + this.message = this.stableMessage + " with instance ID " + error.errorInstanceId(); this.error = error; this.status = status; + this.args = Collections.singletonList(SafeArg.of("errorInstanceId", error.errorInstanceId())); + } + + @Override + public String getMessage() { + return message; } @Override public String getLogMessage() { - return getMessage(); + return stableMessage; } @Override public List> getArgs() { // RemoteException explicitly does not support arguments because they have already been recorded // on the service which produced the causal SerializableError. - return Collections.emptyList(); + return args; } } diff --git a/errors/src/test/java/com/palantir/conjure/java/api/errors/RemoteExceptionTest.java b/errors/src/test/java/com/palantir/conjure/java/api/errors/RemoteExceptionTest.java index fede68c2a..8cc9994df 100644 --- a/errors/src/test/java/com/palantir/conjure/java/api/errors/RemoteExceptionTest.java +++ b/errors/src/test/java/com/palantir/conjure/java/api/errors/RemoteExceptionTest.java @@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.palantir.logsafe.SafeArg; import org.apache.commons.lang3.SerializationUtils; import org.junit.jupiter.api.Test; @@ -72,9 +73,7 @@ public void testLogMessageMessage() { .errorInstanceId("errorId") .build(); RemoteException remoteException = new RemoteException(error, 500); - assertThat(remoteException.getLogMessage()) - .isEqualTo(remoteException.getMessage()) - .isEqualTo("RemoteException: errorCode (errorName) with instance ID errorId"); + assertThat(remoteException.getLogMessage()).isEqualTo("RemoteException: errorCode (errorName)"); } @Test @@ -87,8 +86,6 @@ public void testArgsIsEmpty() { .putParameters("param", "value") .build(), 500); - assertThat(remoteException.getArgs()) - .describedAs("RemoteException does not support parameters by design") - .isEmpty(); + assertThat(remoteException.getArgs()).containsExactly(SafeArg.of("errorInstanceId", "errorId")); } }