Skip to content

Commit c637211

Browse files
authored
SerializableError#params now contains unsafe args in addition to safe args (#65)
* SerializableError#params now contains unsafe args in addition to safe args * Use for loop (200ns) instead of stream collect toMap (260ns)
1 parent 2a68b58 commit c637211

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,16 @@ String getMessage() {
107107

108108
/**
109109
* Creates a {@link SerializableError} representation of this exception that derives from the error code and
110-
* message, as well as the {@link Arg#isSafeForLogging safe} {@link ServiceException#args parameters}.
110+
* message, as well as the {@link Arg#isSafeForLogging safe} and unsafe {@link ServiceException#args parameters}.
111111
*/
112112
public static SerializableError forException(ServiceException exception) {
113113
Builder builder = new Builder()
114114
.errorCode(exception.getErrorType().code().name())
115115
.errorName(exception.getErrorType().name())
116116
.errorInstanceId(exception.getErrorInstanceId());
117+
117118
for (Arg<?> arg : exception.getArgs()) {
118-
if (arg.isSafeForLogging()) {
119-
builder.putParameters(arg.getName(), arg.getValue().toString());
120-
}
119+
builder.putParameters(arg.getName(), arg.getValue().toString());
121120
}
122121

123122
return builder.build();

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,35 @@ public final class SerializableErrorTest {
3535
private static final ObjectMapper mapper = ObjectMappers.newServerObjectMapper();
3636

3737
@Test
38-
public void testExceptionToError() {
38+
public void forException_should_keep_both_safe_and_unsafe_args() {
3939
ErrorType error = ErrorType.FAILED_PRECONDITION;
40-
ServiceException exception =
41-
new ServiceException(error, SafeArg.of("safeKey", 42), UnsafeArg.of("foo", "bar"));
40+
ServiceException exception = new ServiceException(error,
41+
SafeArg.of("safeKey", 42),
42+
UnsafeArg.of("sensitiveInfo", "some user-entered content"));
43+
4244
SerializableError expected = new SerializableError.Builder()
4345
.errorCode(error.code().name())
4446
.errorName(error.name())
4547
.errorInstanceId(exception.getErrorInstanceId())
4648
.putParameters("safeKey", "42")
49+
.putParameters("sensitiveInfo", "some user-entered content")
50+
.build();
51+
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
52+
}
53+
54+
@Test
55+
public void forException_arg_key_collisions_just_use_the_last_one() {
56+
ErrorType error = ErrorType.INTERNAL;
57+
ServiceException exception = new ServiceException(
58+
error,
59+
SafeArg.of("collision", "first"),
60+
UnsafeArg.of("collision", "second"));
61+
62+
SerializableError expected = new SerializableError.Builder()
63+
.errorCode(error.code().name())
64+
.errorName(error.name())
65+
.errorInstanceId(exception.getErrorInstanceId())
66+
.putParameters("collision", "second")
4767
.build();
4868
assertThat(SerializableError.forException(exception)).isEqualTo(expected);
4969
}

0 commit comments

Comments
 (0)