Skip to content

Commit a1e6bda

Browse files
pkoenig10bulldozer-bot[bot]
authored andcommitted
Remove errorType from ServiceException args (#133)
Revert of #92 Now that palantir/conjure-java-runtime#827 has merged and we are logging the error name in the exception mapper, we no longer need the injected arg here. With the injected args, the `ServiceExceptionMapper` logs would have both a `errorName` param and a `throwable0_errorType` param which is confusing and unnecessary. Also note that this parameter has never been logged properly (see #130), so this does not cause a regression in capability.
1 parent 7dc15e2 commit a1e6bda

File tree

4 files changed

+15
-45
lines changed

4 files changed

+15
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static SerializableError forException(ServiceException exception) {
111111
.errorName(exception.getErrorType().name())
112112
.errorInstanceId(exception.getErrorInstanceId());
113113

114-
for (Arg<?> arg : exception.getParameters()) {
114+
for (Arg<?> arg : exception.getArgs()) {
115115
builder.putParameters(arg.getName(), Objects.toString(arg.getValue()));
116116
}
117117

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

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

1919
import com.palantir.logsafe.Arg;
20-
import com.palantir.logsafe.SafeArg;
2120
import com.palantir.logsafe.SafeLoggable;
2221
import java.util.ArrayList;
2322
import java.util.Collections;
@@ -27,7 +26,6 @@
2726

2827
/** A {@link ServiceException} thrown in server-side code to indicate server-side {@link ErrorType error states}. */
2928
public final class ServiceException extends RuntimeException implements SafeLoggable {
30-
public static final String ERROR_TYPE_ARG_NAME = "errorType";
3129

3230
private final ErrorType errorType;
3331
private final List<Arg<?>> args; // unmodifiable
@@ -51,7 +49,7 @@ public ServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>..
5149

5250
this.errorType = errorType;
5351
// Note that instantiators cannot mutate List<> args since it comes through copyToList in all code paths.
54-
this.args = collectArgs(errorType, args);
52+
this.args = copyToUnmodifiableList(args);
5553
this.safeMessage = renderSafeMessage(errorType, args);
5654
this.noArgsMessage = renderNoArgsMessage(errorType);
5755
}
@@ -84,12 +82,19 @@ public List<Arg<?>> getArgs() {
8482
}
8583

8684
/**
87-
* Get Args of this service exception excluding injected/generated Args like errorType.
88-
* @return parameters passed to this ServiceException at construction time
85+
* Deprecated.
86+
*
87+
* @deprecated use {@link #getArgs}.
8988
*/
89+
@Deprecated
9090
public List<Arg<?>> getParameters() {
91-
// errorType is always the first argument, so just slice it out
92-
return args.subList(1, args.size());
91+
return getArgs();
92+
}
93+
94+
private static <T> List<T> copyToUnmodifiableList(T[] elements) {
95+
List<T> list = new ArrayList<>(elements.length);
96+
Collections.addAll(list, elements);
97+
return Collections.unmodifiableList(list);
9398
}
9499

95100
private static String renderSafeMessage(ErrorType errorType, Arg<?>... args) {
@@ -119,11 +124,4 @@ private static String renderSafeMessage(ErrorType errorType, Arg<?>... args) {
119124
private static String renderNoArgsMessage(ErrorType errorType) {
120125
return String.format("ServiceException: %s (%s)", errorType.code(), errorType.name());
121126
}
122-
123-
private static List<Arg<?>> collectArgs(ErrorType errorType, Arg<?>... args) {
124-
List<Arg<?>> argList = new ArrayList<>(args.length + 1);
125-
argList.add(SafeArg.of(ERROR_TYPE_ARG_NAME, errorType));
126-
Collections.addAll(argList, args);
127-
return Collections.unmodifiableList(argList);
128-
}
129127
}

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

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

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

21-
import com.google.common.collect.ImmutableList;
2221
import com.palantir.logsafe.Arg;
2322
import com.palantir.logsafe.SafeArg;
2423
import com.palantir.logsafe.UnsafeArg;
25-
import java.util.List;
2624
import java.util.UUID;
2725
import org.junit.Test;
2826

2927
public final class ServiceExceptionTest {
3028

31-
private static final ErrorType ERROR = ErrorType.create(ErrorType.Code.CUSTOM_CLIENT, "Namespace:MyDesc");
29+
private static final String ERROR_NAME = "Namespace:MyDesc";
30+
private static final ErrorType ERROR = ErrorType.create(ErrorType.Code.CUSTOM_CLIENT, ERROR_NAME);
3231
private static final String EXPECTED_ERROR_MSG = "ServiceException: CUSTOM_CLIENT (Namespace:MyDesc)";
3332

3433
@Test
@@ -41,32 +40,6 @@ public void testExceptionMessageContainsNoArgs_safeLogMessageContainsSafeArgsOnl
4140

4241
assertThat(ex.getLogMessage()).isEqualTo(EXPECTED_ERROR_MSG);
4342
assertThat(ex.getMessage()).isEqualTo(EXPECTED_ERROR_MSG + ": {arg1=foo}");
44-
45-
List<Arg<?>> expectedArgs = ImmutableList.<Arg<?>>builder()
46-
.add(SafeArg.of(ServiceException.ERROR_TYPE_ARG_NAME, ERROR))
47-
.add(args)
48-
.build();
49-
50-
assertThat(ex.getArgs()).isEqualTo(expectedArgs);
51-
}
52-
53-
@Test
54-
public void testExceptionMessageWithNameCollisionWithInjectedArgs() {
55-
Arg<?>[] args = {
56-
SafeArg.of(ServiceException.ERROR_TYPE_ARG_NAME, "foo"),
57-
UnsafeArg.of("arg2", 2),
58-
UnsafeArg.of("arg3", null)};
59-
ServiceException ex = new ServiceException(ERROR, args);
60-
61-
assertThat(ex.getLogMessage()).isEqualTo(EXPECTED_ERROR_MSG);
62-
assertThat(ex.getMessage()).isEqualTo(EXPECTED_ERROR_MSG + ": {errorType=foo}");
63-
64-
List<Arg<?>> expectedArgs = ImmutableList.<Arg<?>>builder()
65-
.add(SafeArg.of(ServiceException.ERROR_TYPE_ARG_NAME, ERROR))
66-
.add(args)
67-
.build();
68-
69-
assertThat(ex.getArgs()).isEqualTo(expectedArgs);
7043
}
7144

7245
@Test

test-utils/src/main/java/com/palantir/conjure/java/api/testing/ServiceExceptionAssert.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public final ServiceExceptionAssert hasType(ErrorType type) {
4141
public final ServiceExceptionAssert hasArgs(Arg<?>... args) {
4242
isNotNull();
4343

44-
// fetch getParameters here to exclude any generated/injected args like 'errorType'
4544
AssertableArgs actualArgs = new AssertableArgs(actual.getParameters());
4645
AssertableArgs expectedArgs = new AssertableArgs(Arrays.asList(args));
4746

0 commit comments

Comments
 (0)