Skip to content
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
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-604.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Arg<?>> args;

/** Returns the error thrown by a remote process which caused an RPC call to fail. */
public SerializableError getError() {
Expand All @@ -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<Arg<?>> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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"));
}
}