Skip to content

Commit b6412b6

Browse files
authored
improvement: move errorInstanceId from exception message into exception args (#604)
move `RemoteException`s errorInstanceId from exception message into exception args
1 parent 05e1bb8 commit b6412b6

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: improvement
2+
improvement:
3+
description: move `RemoteException`s errorInstanceId from exception message into
4+
exception args
5+
links:
6+
- https://github.com/palantir/conjure-java-runtime-api/pull/604

errors/src/main/java/com/palantir/conjure/java/api/errors/RemoteException.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.palantir.conjure.java.api.errors;
1818

1919
import com.palantir.logsafe.Arg;
20+
import com.palantir.logsafe.SafeArg;
2021
import com.palantir.logsafe.SafeLoggable;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -25,8 +26,11 @@
2526
public final class RemoteException extends RuntimeException implements SafeLoggable {
2627
private static final long serialVersionUID = 1L;
2728

29+
private final String message;
30+
private final String stableMessage;
2831
private final SerializableError error;
2932
private final int status;
33+
private final List<Arg<?>> args;
3034

3135
/** Returns the error thrown by a remote process which caused an RPC call to fail. */
3236
public SerializableError getError() {
@@ -39,27 +43,29 @@ public int getStatus() {
3943
}
4044

4145
public RemoteException(SerializableError error, int status) {
42-
super(
43-
error.errorCode().equals(error.errorName())
44-
? String.format(
45-
"RemoteException: %s with instance ID %s", error.errorCode(), error.errorInstanceId())
46-
: String.format(
47-
"RemoteException: %s (%s) with instance ID %s",
48-
error.errorCode(), error.errorName(), error.errorInstanceId()));
49-
46+
this.stableMessage = error.errorCode().equals(error.errorName())
47+
? String.format("RemoteException: %s", error.errorCode())
48+
: String.format("RemoteException: %s (%s)", error.errorCode(), error.errorName());
49+
this.message = this.stableMessage + " with instance ID " + error.errorInstanceId();
5050
this.error = error;
5151
this.status = status;
52+
this.args = Collections.singletonList(SafeArg.of("errorInstanceId", error.errorInstanceId()));
53+
}
54+
55+
@Override
56+
public String getMessage() {
57+
return message;
5258
}
5359

5460
@Override
5561
public String getLogMessage() {
56-
return getMessage();
62+
return stableMessage;
5763
}
5864

5965
@Override
6066
public List<Arg<?>> getArgs() {
6167
// RemoteException explicitly does not support arguments because they have already been recorded
6268
// on the service which produced the causal SerializableError.
63-
return Collections.emptyList();
69+
return args;
6470
}
6571
}

errors/src/test/java/com/palantir/conjure/java/api/errors/RemoteExceptionTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21+
import com.palantir.logsafe.SafeArg;
2122
import org.apache.commons.lang3.SerializationUtils;
2223
import org.junit.jupiter.api.Test;
2324

@@ -72,9 +73,7 @@ public void testLogMessageMessage() {
7273
.errorInstanceId("errorId")
7374
.build();
7475
RemoteException remoteException = new RemoteException(error, 500);
75-
assertThat(remoteException.getLogMessage())
76-
.isEqualTo(remoteException.getMessage())
77-
.isEqualTo("RemoteException: errorCode (errorName) with instance ID errorId");
76+
assertThat(remoteException.getLogMessage()).isEqualTo("RemoteException: errorCode (errorName)");
7877
}
7978

8079
@Test
@@ -87,8 +86,6 @@ public void testArgsIsEmpty() {
8786
.putParameters("param", "value")
8887
.build(),
8988
500);
90-
assertThat(remoteException.getArgs())
91-
.describedAs("RemoteException does not support parameters by design")
92-
.isEmpty();
89+
assertThat(remoteException.getArgs()).containsExactly(SafeArg.of("errorInstanceId", "errorId"));
9390
}
9491
}

0 commit comments

Comments
 (0)