Skip to content

Commit b6ef095

Browse files
authored
ServiceException#getLogMessage does not include args (#12)
1 parent 6be652e commit b6ef095

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

errors/src/main/java/com/palantir/remoting/api/errors/ServiceException.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public final class ServiceException extends RuntimeException implements SafeLogg
3232

3333
private final String errorId = UUID.randomUUID().toString();
3434
private final String safeMessage;
35+
private final String noArgsMessage;
3536

3637
/**
3738
* Creates a new exception for the given error. All {@link com.palantir.logsafe.SafeArg safe} parameters are
@@ -48,12 +49,13 @@ public ServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>..
4849

4950
private ServiceException(ErrorType errorType, @Nullable Throwable cause, List<Arg<?>> args) {
5051
// TODO(rfink): Memoize formatting?
51-
super(safeMessage(errorType, args), cause);
52+
super(renderSafeMessage(errorType, args), cause);
5253

5354
this.errorType = errorType;
5455
// Note that instantiators cannot mutate List<> args since it comes through copyToList in all code paths.
5556
this.args = Collections.unmodifiableList(args);
56-
this.safeMessage = safeMessage(errorType, args);
57+
this.safeMessage = renderSafeMessage(errorType, args);
58+
this.noArgsMessage = renderNoArgsMessage(errorType);
5759
}
5860

5961
/** The {@link ErrorType} that gave rise to this exception. */
@@ -68,18 +70,18 @@ public String getErrorId() {
6870

6971
@Override
7072
public String getMessage() {
73+
// Including safe args here since any logger not configured with safe-logging will log this message.
7174
return safeMessage;
7275
}
7376

7477
@Override
7578
public String getLogMessage() {
76-
return safeMessage;
79+
// Not returning safe args here since the safe-logging framework will log this message + args explicitly.
80+
return noArgsMessage;
7781
}
7882

79-
private static String safeMessage(ErrorType errorType, List<Arg<?>> args) {
80-
String message = errorType.code().name().equals(errorType.name())
81-
? String.format("ServiceException: %s", errorType.code())
82-
: String.format("ServiceException: %s (%s)", errorType.code(), errorType.name());
83+
private static String renderSafeMessage(ErrorType errorType, List<Arg<?>> args) {
84+
String message = renderNoArgsMessage(errorType);
8385

8486
if (args.isEmpty()) {
8587
return message;
@@ -102,6 +104,12 @@ private static String safeMessage(ErrorType errorType, List<Arg<?>> args) {
102104
return builder.toString();
103105
}
104106

107+
private static String renderNoArgsMessage(ErrorType errorType) {
108+
return errorType.code().name().equals(errorType.name())
109+
? String.format("ServiceException: %s", errorType.code())
110+
: String.format("ServiceException: %s (%s)", errorType.code(), errorType.name());
111+
}
112+
105113
@Override
106114
public List<Arg<?>> getArgs() {
107115
return args;

errors/src/test/java/com/palantir/remoting/api/errors/ServiceExceptionTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ public final class ServiceExceptionTest {
3030
private static final String EXPECTED_ERROR_MSG = "ServiceException: CUSTOM (MyDesc)";
3131

3232
@Test
33-
public void testExceptionMessagesContainsSafeArgsOnly() {
33+
public void testExceptionMessageContainsNoArgs_safeLogMessageContainsSafeArgsOnly() {
3434
Arg<?>[] args = {
3535
SafeArg.of("arg1", "foo"),
3636
UnsafeArg.of("arg2", 2),
3737
UnsafeArg.of("arg3", null)};
3838
ServiceException ex = new ServiceException(ERROR, args);
3939

40-
String expectedMessage = EXPECTED_ERROR_MSG + ": {arg1=foo}";
41-
assertThat(ex.getMessage()).isEqualTo(expectedMessage);
42-
assertThat(ex.getLogMessage()).isEqualTo(expectedMessage);
40+
assertThat(ex.getLogMessage()).isEqualTo(EXPECTED_ERROR_MSG);
41+
assertThat(ex.getMessage()).isEqualTo(EXPECTED_ERROR_MSG + ": {arg1=foo}");
4342
}
4443

4544
@Test

0 commit comments

Comments
 (0)